我正在尝试对以下内容进行单元测试
@app.route('/questionSearch', methods=['POST'])
def search():
body = request.get_json()
search_result = Question.query.filter(Question.question.ilike('%'+body['searchTerm']+'%')).all()
total_questions = Question.query.all()
formated_result = [result.format() for result in search_result]
return jsonify({
'success':True,
'questions': formated_result,
'totalQuestions': len(total_questions)
#'currentCategory': categories
})
我测试的方式是:
def test_add_question(self):
res =self.client().post('/questions',
json= {"question": "new Test question?", "answer":"Test answer", "difficulty":1, "category":1 })
data=json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'],True)
它有效,但我如何避免向数据库添加一百万个问题,目前每次运行测试脚本时,它都会向测试数据库添加一个新问题。
我应该在拆卸中写一个删除命令吗?
答案 0 :(得分:0)
您可以尝试使用 mock.patch 重写您的测试以模拟数据库(并完全避免接触数据库),如下所示:
func buildRoundView(roundView: UIView, total : Int, current : Int){
roundView.layer.cornerRadius = roundView.frame.size.width / 2
roundView.backgroundColor = .clear
let width :CGFloat = 10.0
let reducer :CGFloat = 0.010
let circlePath = UIBezierPath(arcCenter: CGPoint (x: roundView.frame.size.width / 2, y: roundView.frame.size.height / 2),
radius: roundView.frame.size.width / 2,
startAngle: CGFloat(-0.5 * Double.pi),
endAngle: CGFloat(1.5 * Double.pi),
clockwise: true)
let multiplier = CGFloat((100.000 / Double(total)) * 0.0100)
for i in 1...total {
let circleShape = CAShapeLayer()
circleShape.path = circlePath.cgPath
if i <= current {
circleShape.strokeColor = UIColor.systemRed.cgColor
}
else{
circleShape.strokeColor = UIColor.lightGray.cgColor
}
circleShape.fillColor = UIColor.clear.cgColor
circleShape.lineWidth = width
circleShape.strokeStart = CGFloat(CGFloat(i - 1) * multiplier) + reducer
circleShape.strokeEnd = CGFloat(CGFloat(i) * multiplier) - reducer
roundView.layer.addSublayer(circleShape)
}
}