https://docs.pytest.org/en/7.1.x/contents.html
Running pytest without mentioning a filename will run all files of format test_*.py or *_test.py in the current directory and subdirectories. Pytest automatically identifies those files as test files. We can make pytest run other filenames by explicitly mentioning them.
*To see the log details, v increases the verbosity
pytest -v
* To execute the tests from a specific file, use the following syntax
pytest <filename> -v
* To execute the tests containing a string in its name we can use the following syntax
pytest -k <substring> -v
EX: Running all tests name include regression, pytest -k regression -v
* To create their custom marker names which to be applied on the tests using the syntax
@pytest.mark.<markername>
EX: Running all smoke tests, @pytest.mark.smoke
* To parameterize tests, example;
@pytest.mark.parametrize("input, output",[(1,11),(2,22),(3,35),(4,44)])
def test_multiplication_11(input, output):
assert 11*input== output
* The syntax to stop the execution of test suite soon after n number of test fails is
pytest --maxfail = <num>
* run tests by using the syntax
pytest -n <num>
EX: pytest -n 3
* To produce a report,
pytest --junitxml="result.xml"
pytest --html=report.html
* A fixture function defined inside a test file has a scope within the test file only. We cannot use that fixture in another test file. To make a fixture available to multiple test files, we have to define the fixture function in a file called conftest.py. conftest.py
Executing all test files using pytest –v.
Executing specific file usimng pytest <filename> -v.
Execute tests by substring matching pytest -k <substring> -v.
Execute tests based on markers pytest -m <marker_name> -v.
Creating fixtures using @pytest.fixture.
conftest.py allows accessing fixtures from multiple files.
Parametrizing tests using @pytest.mark.parametrize.
Xfailing tests using @pytest.mark.xfail.
Skipping tests using @pytest.mark.skip.
Stop test execution on n failures using pytest --maxfail <num>.
Running tests in parallel using pytest -n <num>.
Generating results xml using pytest -v --junitxml = "result.xml".
Pytest Resources: