Exploring pytest command line options

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.


Installation:

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.

2pytest–version

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.

6pytest –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.

f- failed

E- error

s- skipped

x- xfailed

X- xpassed

p- passed

P-passed with output

a- all except pP

A- all

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.

12pytest –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.


4 thoughts on “Exploring pytest command line options

  1. Thanks for the info. I’m surprise these are not officially listed in Pytest documentation.
    BUT: all the screen captures makes it really hard to read and browse thru when looking for a specific option. Maybe add a table of content? Or keep the web-page background color a shade close to the screen capture. Scrolling this page may induce epilepsy 🙂

    1. Hello,
      For that, you need to install xdist – pytest-xdist. The benefit of using this is it will scale your tests run to multiple machines. Please refer
      https://stackoverflow.com/questions/29958823/pytest-run-each-test-as-a-mutlitprocessing-process
      https://pypi.org/project/pytest-xdist/
      In the Blog 17 step mentioned – This will speed up tests by sending tests to multiple CPUs.
      let us know if there are any further questions.

      Regards,
      Raghava

Leave a Reply

Your email address will not be published. Required fields are marked *