Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pytest,如何针对任一夹具进行测试';价值是多少?_Python_Integration Testing_Pytest - Fatal编程技术网

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):
    ...