In this lesson, you will learn:

  • About the main command-line options to use when working with Pytest.

The Pytest provides a number of different options. We will now cover the main CLI options that you will find useful when working with Pytest.

Collect Only

The first CLI option we will look at is --collect-only. This option does not execute the tests but only collects them. Example below:

❯ pytest 002_getting_started/001_creating_first_test/ --collect-only
====================================================================== test session starts ======================================================================
platform linux -- Python 3.8.5, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /home/rick/development/course-dev/pytest-course/NEW/002_getting_started/001_creating_first_test
plugins: cases-3.6.4, metadata-1.11.0, html-3.1.1, lazy-fixture-0.6.3
collected 2 items                                                                                                                                               

<Module test_001_vlan.py>
  <Function test_vlan>
<Module test_002_mtu.py>
  <Function test_mtu>

Verbose

Should we want to increase verbosity to our output, we can use the -v option. When looking at our example output below, the useful additions to the output that this gives us are:

  • The previously supplied . and F for pass and failures are expanded to FAILED and PASSED.
  • The path of our test is expanded to show the module file, class (if present) and function name. For example: test_001_vlan.py::test_vlan.
❯ pytest -v 002_getting_started/001_creating_first_test/
=============================================================== test session starts ================================================================
platform linux -- Python 3.8.5, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /home/rick/development/course-dev/pytest-course/venv/bin/python3
cachedir: .pytest_cache
metadata: {'Python': '3.8.5', 'Platform': 'Linux-5.10.60.1-microsoft-standard-WSL2-x86_64-with-glibc2.29', 'Packages': {'pytest': '6.2.5', 'py': '1.10.0', 'pluggy': '1.0.0'}, 'Plugins': {'metadata': '1.11.0', 'freezegun': '0.4.2', 'html': '3.1.1'}}
rootdir: /home/rick/development/course-dev/pytest-course/002_getting_started/001_creating_first_test
plugins: metadata-1.11.0, freezegun-0.4.2, html-3.1.1
collected 2 items                                                                                                                                  

test_001_vlan.py::test_vlan FAILED                                                                                                           [ 50%]
test_002_mtu.py::test_mtu PASSED                                                                                                             [100%]

===================================================================== FAILURES =====================================================================
____________________________________________________________________ test_vlan _____________________________________________________________________

    def test_vlan():
        device_vlan = "100"
        expected_vlan = "101"
    
>       assert device_vlan == expected_vlan
E       AssertionError: assert '100' == '101'
E         - 101
E         + 100

test_001_vlan.py:5: AssertionError
============================================================= short test summary info ==============================================================
FAILED test_001_vlan.py::test_vlan - AssertionError: assert '100' == '101'
=========================================================== 1 failed, 1 passed in 0.11s ============================================================
Ready to Master Network Automation? Start Your Journey Today!
Our membership provides:
  • Full deep-dive course library (inc. Batfish, pyATS, Netmiko)
  • Code repositories inc. full course code, scripts and examples
  • 24x7 multi-vendor labs (Arista, Cisco, Juniper)
  • Private online community
  • Live monthly tech sessions
  • Access to tech session library

Join Now ➜
Close You've successfully subscribed to Packet Coders.
Close Success! Your account is fully activated, you now have access to all content.
Close Welcome back! You've successfully signed in.
Close Nearly there! To activate your account, please click the link in the email we just sent you.