Invoking pytest from CLI

Introduzione al Testing in Python

Alexander Levin

Data Scientist

Example CLI run: syntax

Command-Line Interface (CLI) - a user interface that allows to interact with a computer program by entering text commands into a terminal.

The command for running the slides.py from CLI:

pytest slides.py

Meaning: "Please, run the pytest framework using the tests from the slides.py module"

Introduzione al Testing in Python

Example CLI run: output

Output of a test:

test output example

Introduzione al Testing in Python

Example CLI run: output

Output of a test:

  • Modules versions

test output example - versions highligted

Introduzione al Testing in Python

Example CLI run: output

Output of a test:

  • Number of "collected" tests

test output example - collected highligted

Introduzione al Testing in Python

Example CLI run: output

Output of a test:

  • Names of test scripts

test output example - names of test scripts highligted

Introduzione al Testing in Python

Example CLI run: output

Output of a test:

  • Test results

test output example - results highligted

Introduzione al Testing in Python

IDE exercises

IDE example

Introduzione al Testing in Python

IDE exercises

IDE example with arrows

Introduzione al Testing in Python

Directory argument

The command for running all tests in tests_dir/:

pytest tests_dir/

Meaning: "Please, run the pytest framework using all found the tests from the tests_dir folder".

directory tests output

Introduzione al Testing in Python

Keyword argument - filter tests by name

The command for running tests from tests_ex.py contains "squared":

pytest tests_ex.py -k "squared"

Meaning: "Please, run the pytest framework using all tests from the tests_ex.py script containing squared".

Output: keyword tests output

Introduzione al Testing in Python

Summary

  • IDE exercises - let us to write code in an Integrated Development Environment and to use command-line interface (CLI)
  • CLI pytest command starts with pytest
  • Sources of tests:
    • One script, by passing script_name.py
    • A set of scripts from one folder, by passing directory_name/
  • Keyword argument:
    • By passing -k "keyword_expression"
  • Output of a test contains important information about the run
Introduzione al Testing in Python

Let's practice!

Introduzione al Testing in Python

Preparing Video For Download...