Monday, October 3, 2016

Pytest Fixtures

When setting up automated testing, I use pytest. It has an interesting feature called fixtures.

The idea with fixtures is that you define a function, labeled as a fixture, and then you can pass that function as an argument to tests. The function will be executed at the beginning of the test.

For example, this function is a fixture:

@pytest.fixture(scope="function")
def testdir():
    # always run in test dir
    os.chdir(os.path.dirname(os.path.realpath(__file__)))

To run this function at the beginning of the test, just pass testdir as a parameter to the test:

 def test_run_no_history(testdir, tmpdir, s3_no_history_file, input_file, expected_output, config_file):
    base_dir = str(tmpdir.chdir())
By passing testdir as the first argument, I cause pytest to run the function at the beginning of the test.

No comments:

Post a Comment