Pytest

Usage

pytest [options] [file_or_dir] [file_or_dir] ...

Help

pytest --help|zless

Options

 -s                    Show Output, do not caputure
 -x                    Stop after first failure
 -k "expression"       Only run tests that match expession (and fixtures)
 -rs                   Show extra summary info for SKIPPED
 -r chars              Show extra test summary info as specified by chars:
                       (f)ailed, (E)error, (s)skipped, (x)failed, (X)passed
                       (w)pytest-warnings (p)passed, (P)passed with output,
                       (a)all except pP.

 -v                    Verbose
 -q, --quiet           Less verbose

 -l, --showlocals      Show local variables in tracebacks

Pass arguments in Pytest by command line

conftest.py

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

test_param.py

import pytest

@pytest.fixture(scope="session")
def name(pytestconfig):
    return pytestconfig.getoption("name")

def test_print_name(name):
        print(f"\ncommand line param (name): {name}")

def test_print_name_2(pytestconfig):
    print(f"test_print_name_2(name): {pytestconfig.getoption('name')}")

Running test with input arguments

pytest -q -s --name Brian test_param.py

Result

================================================ test session starts =================================================
platform linux -- Python 3.9.12, pytest-7.1.2, pluggy-1.0.0
rootdir: /home/derek/Desktop/zz
collected 2 items                                                                                                    

test_param.py 
command line param (name): Brian
.test_print_name_2(name): Brian
.

================================================= 2 passed in 0.00s ==================================================

Reference

pytest cheat sheet
pytest cheat sheet. GitHub Gist: instantly share code, notes, and snippets.
How to pass arguments in pytest by command line
I have a code and I need to pass the arguments like name from terminal.Here is my code and how to pass the arguments. I am getting a “File not found” kind error that I don’t understand. I have tr...