Skip to content

This file contains examples of how to write tests using pytest!

Some good practices for writting great Python tests:

Source: https://www.nerdwallet.com/blog/engineering/5-pytest-best-practices/

  • Prefer mocker over mock
  • Parametrize the same behavior, have different tests for different behaviors
  • Don’t modify fixture values in other fixtures
  • Prefer responses over mocking outbound HTTP requests
  • Prefer tmpdir over global test artifacts
Classes
  • TestGroup A class with common parameters, param1 and param2.</>
Functions
  • test_divide_error(a, b, expected) Check if divide returns correct Exceptions for known entries.</>
  • test_divide_ok(a, b, expected) Check if divide works for expected entries.</>

A class with common parameters, param1 and param2.

Methods
  • fixt() (int) This fixture will only be available within the scope of TestGroup.</>
  • test_one(param1, param2, fixt) Run the first test using the fixture.</>
method

fixt()

This fixture will only be available within the scope of TestGroup.

Returns (int)

A common value to be used by multiple tests

method

test_one(param1, param2, fixt)

Run the first test using the fixture.

Parameters
  • param1 (str) First parameter.
  • param2 (str) Second parameter.
  • fixt (int) Value from fixture.

Check if divide works for expected entries.

Parameters
  • a (float) Dividend.
  • b (float) Divisor.
  • expected (float) expected result.

Check if divide returns correct Exceptions for known entries.

Issue raised by https://github.com/nullhack/python-project-example/issues/1337

Parameters
  • a (float) Dividend.
  • b (float) Divisor.
  • expected (Exception) expected Exception.