Python Pytest,如何针对任一夹具进行测试';价值是多少?
我有一个带有一个夹具的测试用例: 但除了针对一些Python Pytest,如何针对任一夹具进行测试';价值是多少?,python,integration-testing,pytest,Python,Integration Testing,Pytest,我有一个带有一个夹具的测试用例: 但除了针对一些用户对象进行测试外,我还想针对无对象进行测试(我希望测试不会失败)。我想我可以做一些类似的事情: @pytest.fixture(params=[True, False]) def User_or_null(test_client, request): if request.param: return User.objects.first() else: return None 但是我认为这不允许我
用户
对象进行测试外,我还想针对无
对象进行测试(我希望测试不会失败)。我想我可以做一些类似的事情:
@pytest.fixture(params=[True, False])
def User_or_null(test_client, request):
if request.param:
return User.objects.first()
else:
return None
但是我认为这不允许我用
pytest.mark.xfail
为None
值标记测试用例?有什么想法吗?我认为参数化用户
夹具没有问题。您可以通过标记单独的参数,例如:
@pytest.fixture(params=[
'testuser',
# wrap None into pytest.param to treat it specially
pytest.param(None, marks=pytest.mark.xfail)
])
def user(request):
if request.param is None:
return None
return User.objects.filter(name=request.param).first() # or whatever
但是,这意味着使用用户
夹具的所有测试都将在无
上xfail/xpass-这可能不是所有测试都需要的。如果只希望完成选定的测试,请使用间接参数化:
# user fixture is not parametrized now
@pytest.fixture
def user(request):
if request.param is None:
return None
return User.objects.filter(name=request.param).first()
# instead, parametrizing is done from the test:
@pytest.mark.parametrize('content', ['nice post',])
@pytest.mark.parametrize('user', [
'testuser',
pytest.param(None, marks=pytest.mark.xfail
)], indirect=True)
def test_post(test_client, user, content):
...
# user fixture is not parametrized now
@pytest.fixture
def user(request):
if request.param is None:
return None
return User.objects.filter(name=request.param).first()
# instead, parametrizing is done from the test:
@pytest.mark.parametrize('content', ['nice post',])
@pytest.mark.parametrize('user', [
'testuser',
pytest.param(None, marks=pytest.mark.xfail
)], indirect=True)
def test_post(test_client, user, content):
...