Invoking pytest from CLI

Introduction to 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"

Introduction to Testing in Python

Example CLI run: output

Output of a test:

test output example

Introduction to Testing in Python

Example CLI run: output

Output of a test:

  • Modules versions

test output example - versions highligted

Introduction to Testing in Python

Example CLI run: output

Output of a test:

  • Number of "collected" tests

test output example - collected highligted

Introduction to Testing in Python

Example CLI run: output

Output of a test:

  • Names of test scripts

test output example - names of test scripts highligted

Introduction to Testing in Python

Example CLI run: output

Output of a test:

  • Test results

test output example - results highligted

Introduction to Testing in Python

IDE exercises

IDE example

Introduction to Testing in Python

IDE exercises

IDE example with arrows

Introduction to 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

Introduction to 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

Introduction to 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
Introduction to Testing in Python

Let's practice!

Introduction to Testing in Python

Preparing Video For Download...