[PYTHON] Make fixture an argument to parameterize in py.test

Introduction

The pytest fixture is a very useful tool when you want to repeat the test under the same conditions. When I wanted to use fixture as a test parameter to write a test that is common to various conditions, the information was unexpectedly not gathered on the net, so I will describe it here.

By the way, for information on how to use fixture as a test parameter, see Official Page and GitHub. / pytest-dev / pytest / issues / 349), so it's possible that a slightly easier way will be offered in the near future.

Thing you want to do

import pytest

@pytest.fixture
def fixture_a():
    yield 1

@pytest.fixture
def fixture_b():
    yield 2

@pytest.fixture
def fixture_c():
    yield 3

@pytest.mark.parametrize("expected, generated", [
        (1, fixture_a),
        (2, fixture_b),
        (5, fixture_c),
])
def test_fail(expected, generated):
    assert expected == generated

However, this doesn't work.

Alternative

Put together fixtures by using the decorator pytest.fixture.

test.py


#Other functions are the same as above

@pytest.fixture(params=['a', 'b', 'c'])
def context(request):
    if request.param == 'a':
        return (1, request.getfixturevalue('fixture_a'))
    elif request.param == 'b':
        return (2, request.getfixturevalue('fixture_b'))
    elif request.param == 'c':
        return (4, request.getfixturevalue('fixture_c'))

def test_fixture_parameterize(context):
    expected, generated = context
    assert expected == generated
$ pytest test.py
========================================= test session starts =========================================
platform linux2 -- Python 2.7.12, pytest-3.1.0, py-1.4.33, pluggy-0.4.0
rootdir: /home/koreyou/work/pytest_fixture_parameterize, inifile:
collected 3 items 

test.py ..F

============================================== FAILURES ===============================================
____________________________________ test_fixture_parameterize[c] _____________________________________

context = (4, 3)

    def test_fixture_parameterize(context):
        expected, generated = context
>       assert expected == generated
E       assert 4 == 3

test.py:31: AssertionError
================================= 1 failed, 2 passed in 0.02 seconds ==================================

It fails as expected.

Recommended Posts

Make fixture an argument to parameterize in py.test
How to make an interactive CLI tool in Golang
An alternative to `pause` in Python
How to make a string into an array or an array into a string in Python
I want to make an automation program!
How to make an embedded Linux device driver (11)
How to make an embedded Linux device driver (1)
How to make an embedded Linux device driver (4)
How to make an embedded Linux device driver (7)
How to make an embedded Linux device driver (3)
[Blender x Python] How to make an animation
How to make an embedded Linux device driver (6)
How to make an embedded Linux device driver (5)
How to make an embedded Linux device driver (10)
How to make Python Interpreter changes in Pycharm
How to Mock a Public function in Pytest
How to make an embedded Linux device driver (9)
Tool to make mask image for ETC in Python
Try to make a Python module in C language
Make each PowerPoint page an image file in Python
How to make Yubico Yubikey recognized in Manjaro Linux
Explain in detail how to make sounds with python
How to make an HTTPS server with Go / Gin
How to create an image uploader in Bottle (Python)
How to make an embedded Linux device driver (12) (Complete)
How to use Decorator in Django and how to make it
I tried to make an OCR application with PySimpleGUI
How to specify an infinite number of tolerances in the numeric argument validation check of argparse
If you want to put an argument in the closure function and execute it later