This post will serve as a quick tutorial for using pytest command-line options. Anyone who is new to pytest will benefit from this article.
Pytest comes with many useful command-line options right out of the box. When I started learning pytest, I had to search through a lot of material available on the official pytest website as well as other Internet resources. I hope this tutorial will help any newbie, who wants to start using pytest commands effectively.
1. Run the following command in your command line:
2. Check the installed version.
Pytest command line options
Note: Qxf2 Page object model has been used to demonstrate the examples in the post.
1. python -m pytest
You can invoke testing through python interpreter from the command line.
This gives you a version of the pytest module.
3. pytest –fixtures
This will give you available fixtures
4. pytest -h or pytest –help
This shows help on command line and config-line options
5. pytest -x
This command stops execution after the first failure.
6. pytest –maxfail=n
This command stops execution after nth failure. n can be any number such as 1.2.3
7. pytest module name
This command helps to run tests by specifying the module name.
8. pytest directory_name/
This command runs the tests from the directory
9. pytest -k “keyword expression”
This command runs the test based on the keyword expression. e.g. in the below example, we will be running only API tests not bitcoin tests from our suite.
10. Detailed summary report options
The -r flag can be used to display a short summary report at the end of the test. Various options are discussed in the below section.
10.1 pytest -rA
This gives output of all tests
Below is the full list of characters available.
P-passed with output
a- all except pP
More than one character can be used so in the below example failed and skipped tests can be seen after running following command
10.2 pytest -rfs
11. pytest –ignore=ignorepaths during tests
You can specify module, directory or tests to ignore the tests.
In the below screen you can see the test has been ignored from the execution.
12. pytest –collect-only
You can peep into the collections without actually executing the tests.
13. pytest -m <markername>
Markers that have been defined in the test_example_table.py file and pytest module imported in the test file as shown below.
Only the tests defined by the marker will run after this command.
14. pytest –junitxml=”xmlfilename”
After running this command pytest will generate XML report
15. py.test -s
Users can influence output capturing mechanisms from the command line, where -s option disables all capturing.
16. py.test -E
Users can create custom marker and command-line options before running tests following additions to be done in the conftest.py file and test file
In the conftest.py file
In the test_example_form.py
Then run the test it will give output as shown in the below screen.
17. pytest -n NUM
This will speed up tests by sending tests to multiple CPUs. This can be useful in longer running tests or tests requiring a lot of I/O. This can lead to considerable speedups.
I hope you have liked the blog. There are many more useful pytest commands that can be used by testers or developers for their testing. This post captures the most commonly used commands in our experience. A complete list of commands will be available at pytest documentation.