问题描述
我目前在flask应用程序中具有一组pytest单元测试,其方式如下。我已经设置了测试多个场景的装置。它们也分为多个类,以测试不同的API端点。
但是,请注意,许多固定装置使用相同的测试/登录功能,并且总体而言,测试似乎过时了。您能否推荐最佳实践来对以下内容进行干燥/重构?我知道您可以在一个测试中执行多个断言,但是我觉得将它们分开可以让您看到测试在什么时候失败更好。所有建议表示赞赏。
class TestSetone:
# Test response for successful matches
@pytest.fixture(scope="class")
def matching_data(self):
test = app.test_client(self)
token = login("111","asdfasdf")
data = {
"phoneNumber": "+1 555-412-1235"
}
response = test.post(
'/phone_search',headers={
"Authorization": "Bearer {}".format(token)
},json=data
)
return response
def test_matching_data_status_code(self,matching_data):
status_code = matching_data.status_code
assert status_code == 200
def test_matching_data_type(self,matching_data):
response_data = matching_data.json
assert isinstance(response_data,list)
def test_matching_data_content(self,matching_data):
response_data = matching_data.json
assert response_data is not None
# Test response for no matches
@pytest.fixture(scope="class")
def no_match_data(self):
test = app.test_client(self)
token = login("111","asdfasdf")
data = {
"phoneNumber": "1234567890"
}
response = test.post(
'/phone_search',json=data
)
return response
def test_no_match_data_status_code(self,no_match_data):
status_code = no_match_data.status_code
assert status_code == 200
def test_no_match_data_type(self,no_match_data):
response_data = no_match_data.json
assert isinstance(response_data,list)
def test_no_match_data_content(self,no_match_data):
response_data = no_match_data.json
assert response_data == []
# Test bad input data response
@pytest.fixture(scope="class")
def invalid_data(self):
test = app.test_client(self)
token = login("111","asdfasdf")
data = {
"phoneNumber": 17185783339
}
response = test.post(
'/phone_search',headers={
"Authorization": "Bearer {}".format(token)
}
)
return response
def test_invalid_data_status_code(self,invalid_data):
status_code = invalid_data.status_code
assert status_code == 400
def test_invalid_data_content(self,invalid_data):
response_data = invalid_data.json
assert response_data is None
# Test no input data response
@pytest.fixture(scope="class")
def no_data(self):
test = app.test_client(self)
token = login("111","asdfasdf")
response = test.post(
'/phone_search',headers={
"Authorization": "Bearer {}".format(token)
}
)
return response
def test_no_data_status_code(self,no_data):
status_code = no_data.status_code
assert status_code == 400
def test_no_data_content(self,no_data):
response_data = no_data.json
assert response_data is None
# Test for Unauthorized
@pytest.fixture(scope="class")
def no_auth(self):
test = app.test_client(self)
data = {
"phoneNumber": "+1 555-412-1235"
}
response = test.post(
'/phone_search',json=data
)
return response
def test_no_auth_status_code(self,no_auth):
status_code = no_auth.status_code
assert status_code == 401
class TestSetTwo:
@pytest.fixture(scope="class")
def no_auth(self):
test = app.test_client(self)
token = login("111","asdfasdf")
data = {
"user_id": "asdfasdf"
}
response = test.post(
'/user_search',json=data
)
return response
~ etc ~
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)