question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

parametrized pytest - how to run concurrently

See original GitHub issue

Hey,

I have a parametrize snippet looking like:

scenarios: List[Tuple[str, Dict[str, str]]] = load_scenarios()

for scenario in scenarios:
    idlist.append(scenario[0])
    items = scenario[1].items()
    argnames = [item[0] for item in items]
    argvalues.append([item[1] for item in items])
metafunc.parametrize(argnames, argvalues, ids=idlist, scope='class')

This collects 263 total tests. I am going to add a few other tests and the total collected with scenarios will ballon to ~780. Don’t ask. Each indidivual test takes about 1-2.5s. Based on this parametrize collection, pytest-parallel and xdist are not gonna work, at least not so far as I’ve tried testing. How might I actually get these concurrent?

Also - each of the 263 tests spawns a Popen.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
asottilecommented, Aug 30, 2019

yeah, my point was that mutating pytest.setup_completed will not be reflected in other processes

0reactions
artis3ncommented, Aug 30, 2019

Ok, got it working after removing the nonce. Full snippets for future people that come to this issue:

@pytest.fixture(autouse=True, scope='session')
def all_prep(dependency_dir, global_dependency_dir) -> None:
    """
    Runs all pre- and post-test setup and teardown.
    :param dependency_dir: Path to the dependency directory for this test session
    :param global_dependency_dir: Path to the inter-session dependency directory
    :return: None
    """
    this_worker = os.environ['PYTEST_XDIST_WORKER']
    setup_complete_file = dependency_dir / 'setup_complete'  # pathlib.Path

    # Only one worker should run the setup. The rest will block until it is complete.
    if this_worker == 'gw0':
        # Setup
        server = setup_server(dependency_dir, global_dependency_dir)
        setup_other_stuff(global_dependency_dir)
        complete_setup(setup_complete_file)

        try:
            yield  # Return execution to parent pytest method, run test suite, then move to the 'finally' block
        finally:
            # Teardown
            teardown_server(server)
            setup_complete_file.unlink()  # Delete the setup_complete file

    else:
        wait_for_setup_to_complete(setup_complete_file)
        yield  # Worker returns execution to parent to run test suite


def complete_setup(complete: Path):
    complete.touch()


def wait_for_setup_to_complete(complete: Path):
    # Block until setup has been completed
    while not complete.exists():
        sleep(2)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Parallely running parameterized tests in pytest - Stack Overflow
This is for a concurrency testing scenario. Same testcase runs in parallel with different parameters in a device. After completing all the  ......
Read more >
Parametrizing tests — pytest documentation
Let's say we want to execute a test with different computation parameters and the parameter range shall be determined by a command line...
Read more >
pytest tricks: Parametrize Twice - Calmcode
In pytest, you're able to stack parametrize calls. ... You can test for many values in pytest in a single test by using...
Read more >
Part 6 - How to Parametrize test in PyTest (Parameterization)
In this video, I have explained how to paramterize test cases with different data in pytest.Parameterizing of a test is done to run...
Read more >
Pytest - Run Tests in Parallel - Tutorialspoint
By default, pytest runs tests in sequential order. In a real scenario, a test suite will have a number of test files and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found