How to mention file name in nox + pytest?

655 views Asked by At

My nox function looks something like this

@nox.session(python=["3.7", "3.8", "3.9"])
def test(session: nox.Session) -> None:
    """Run unit tests."""
    session.install("-e", ".[all]")
    session.install("-e", ".[tests]")
    print (session.posargs)
    session.run("pytest", *session.posargs)

How to update the above function to run tests of specific files or file patterns like tests/*/test_postgres_*.py

1

There are 1 answers

0
baulin On

Here’s a quick example that demonstrates how to use arguments to run tests against a particular file:

@nox.session
def test(session):
    session.install('pytest')

    if session.posargs:
        test_files = session.posargs
    else:
        test_files = ['test_a.py', 'test_b.py']

    session.run('pytest', *test_files)

you run:

nox -- test_c.py

Then nox will run:

pytest test_c.py

nox posargs