Danijel
Danijel

Reputation: 8606

Possible to have pytest within pytest?

I made a pytest which tests all files in given directory.

@pytest.mark.dir
def test_dir(target_dir):
    for filename in os.listdir(target_dir):
        test_single(filename)

def test_single(filename):
    ...
    ...
    assert( good or bad )

The target_dir is supplied from command line:
pytest -m dir --target_dir=/path/to/my_dir

pytest_addoption() is used to parse the command line (code is ommited for clarity).

The output from the test gives single pass/fail mark even though test_single() runs hudreds of times. Would it be possible to get a pass/fail mark for each file?

Upvotes: 1

Views: 40

Answers (1)

tmt
tmt

Reputation: 8604

I think the way to go is to parametrize your test function so that target_dir is effectively split into individual files in a fixture filename:

# conftest.py

import os

def pytest_addoption(parser):
    parser.addoption("--target_dir", action="store")

def pytest_generate_tests(metafunc):
    option_value = metafunc.config.option.target_dir

    if "filename" in metafunc.fixturenames and option_value is not None:
        metafunc.parametrize("filename", os.listdir(option_value))
# test.py

import pytest

@pytest.mark.dir
def test_file(filename):
    # insert your assertions
    pass

Upvotes: 1

Related Questions