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


pytest --help|zless


 -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

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

import pytest

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 session starts =================================================
platform linux -- Python 3.9.12, pytest-7.1.2, pluggy-1.0.0
rootdir: /home/derek/Desktop/zz
collected 2 items                                                                                             
command line param (name): Brian
.test_print_name_2(name): Brian

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


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...