Python run module tests

Organizing Code & Running unittest

Summary: in this tutorial, you’ll learn how to organize the test code and how to use the various commands to run unit tests.

Organizing code

If you have a few modules, you can create test modules and place them within the same directory.

In practice, you may have many modules organized into packages. Therefore, it’s important to keep the development code and test code more organized.

It’s a good practice to keep the development code and the test code in separate directories. And you should place the test code in a directory called test to make it obvious.

For demonstration purposes, we’ll create a sample project with the following structure:

D:\python-unit-testing ├── shapes | ├── circle.py | ├── shape.py | └── square.py └── test ├── test_circle.py ├── test_square.py └── __init__.pyCode language: Python (python)

First, create the shapes and test directories in the project folder ( python-unit-testing ).

Second, create three modules shape.py , circle.py , and square.py modules and place them in the shapes directory.

from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area() -> float: passCode language: Python (python)

The Shape class is an abstract class that has the area() method. It is the base class of the Circle and Square class.

import math from .shape import Shape class Circle(Shape): def __init__(self, radius: float) -> None: if radius < 0: raise ValueError('The radius cannot be negative') self._radius = radius def area(self) -> float: return math.pi * math.pow(self._radius, 2)Code language: Python (python)

The Circle class also inherits from the Shape class. It implements the area() method that returns the area of the circle.

import math from .shape import Shape class Square(Shape): def __init__(self, length: float) -> None: if length < 0: raise ValueError('The length cannot be negative') self._length = length def area(self) -> float: return math.pow(self._length, 2)Code language: Python (python)

Like the Circle class, the Square class has the area() method that returns the area of the square.

Third, create the test_circle.py and test_square.py test modules and place them in the test folder:

import unittest import math from shapes.circle import Circle from shapes.shape import Shape class TestCircle(unittest.TestCase): def test_circle_instance_of_shape(self): circle = Circle(10) self.assertIsInstance(circle, Shape) def test_create_circle_negative_radius(self): with self.assertRaises(ValueError): circle = Circle(-1) def test_area(self): circle = Circle(2.5) self.assertAlmostEqual(circle.area(), math.pi * 2.5*2.5)Code language: Python (python)

The test_circle module uses the Circle and Shape from the circle and shape test modules in the shapes package.

import unittest from shapes.square import Square from shapes.shape import Shape class TestSquare(unittest.TestCase): def test_create_square_negative_length(self): with self.assertRaises(ValueError): square = Square(-1) def test_square_instance_of_shape(self): square = Square(10) self.assertIsInstance(square, Shape) def test_area(self): square = Square(10) area = square.area() self.assertEqual(area, 100)Code language: Python (python)

The test_square module uses the Square and Shape classes from the square and shape modules in the shapes package.

It’s important to create an __init__.py file and place it in the test folder. Otherwise, commands in the following section won’t work as expected.

Running unit tests

The unittest module provides you with many ways to run unit tests.

1) Running all tests

To run all tests in the test directory, you execute the following command from the project directory folder ( python-unit-testing ):

python -m unittest discover -vCode language: Python (python)

The discover is a subcommand that finds all the tests in the project.

test_area (test_circle.TestCircle) . ok test_circle_instance_of_shape (test_circle.TestCircle) . ok test_create_circle_negative_radius (test_circle.TestCircle) . ok test_area (test_square.TestSquare) . ok test_create_square_negative_length (test_square.TestSquare) . ok test_square_instance_of_shape (test_square.TestSquare) . ok ---------------------------------------------------------------------- Ran 6 tests in 0.002s OKCode language: Python (python)

2) Running a single test module

To run a single test module, you use the following command:

python -m unittest test_package.test_module -vCode language: Python (python)

For example, the following execute all tests in the test_circle module of the test package:

python -m unittest test.test_circle -vCode language: Python (python)
test_area (test.test_circle.TestCircle) . ok test_circle_instance_of_shape (test.test_circle.TestCircle) . ok test_create_circle_negative_radius (test.test_circle.TestCircle) . ok ---------------------------------------------------------------------- Ran 3 tests in 0.000s OKCode language: Python (python)

3) Running a single test class

A test module may have multiple classes. To run a single test class in a test module, you use the following command:

python -m unittest test_package.test_module.TestClass -vCode language: Python (python)

For example, the following command tests the TestSquare class from the test_square module of the test package:

python -m unittest test.test_circle.TestCircle -vCode language: Python (python)
test_area (test.test_square.TestSquare) . ok test_create_square_negative_length (test.test_square.TestSquare) . ok test_square_instance_of_shape (test.test_square.TestSquare) . ok ---------------------------------------------------------------------- Ran 3 tests in 0.001s OKCode language: Python (python)

4) Running a single test method

To run a single test method of a test class, you use the following command:

python -m unittest test_package.test_module.TestClass.test_method -vCode language: Python (python)

For example, the following command tests the test_area() method of the TestCircle class:

python -m unittest test.test_circle.TestCircle.test_area -vCode language: Python (python)
test_area (test.test_circle.TestCircle) . ok ---------------------------------------------------------------------- Ran 1 test in 0.001s OKCode language: Python (python)

Summary

  • Place the development code and test code in separate directories. It’s a good practice to store the test code in the test directory.
  • Use the command python -m unittest discover -v to discover and execute all the tests.
  • Use the command python -m unittest test_package.test_module -v to run a single test module.
  • Use the command python -m unittest test_package.test_module.TestClass -v to run a single test class.
  • Use the command python -m unittest test_package.test_module.TestClass.test_method -v to run a single test method.

Источник

How to invoke pytest¶

In general, pytest is invoked with the command pytest (see below for other ways to invoke pytest ). This will execute all tests in all files whose names follow the form test_*.py or \*_test.py in the current directory and its subdirectories. More generally, pytest follows standard test discovery rules .

Specifying which tests to run¶

Pytest supports several ways to run and select tests from the command-line.

Run tests in a module

Run tests in a directory

Run tests by keyword expressions

pytest -k 'MyClass and not method' 

This will run tests which contain names that match the given string expression (case-insensitive), which can include Python operators that use filenames, class names and function names as variables. The example above will run TestMyClass.test_something but not TestMyClass.test_method_simple . Use «» instead of » in expression when running this on Windows

Run tests by node ids

Each collected test is assigned a unique nodeid which consist of the module filename followed by specifiers like class names, function names and parameters from parametrization, separated by :: characters.

To run a specific test within a module:

pytest test_mod.py::test_func

Another example specifying a test method in the command line:

pytest test_mod.py::TestClass::test_method

Run tests by marker expressions

Will run all tests which are decorated with the @pytest.mark.slow decorator.

Run tests from packages

This will import pkg.testing and use its filesystem location to find and run tests from.

Getting help on version, option names, environment variables¶

pytest --version # shows where pytest was imported from pytest --fixtures # show available builtin function arguments pytest -h | --help # show help on command line and config file options 

Profiling test execution duration¶

To get a list of the slowest 10 test durations over 1.0s long:

pytest --durations=10 --durations-min=1.0

By default, pytest will not show test durations that are too small (<0.005s) unless -vv is passed on the command-line.

Managing loading of plugins¶

Early loading plugins¶

You can early-load plugins (internal and external) explicitly in the command-line with the -p option:

The option receives a name parameter, which can be:

  • A full module dotted name, for example myproject.plugins . This dotted name must be importable.
  • The entry-point name of a plugin. This is the name passed to setuptools when the plugin is registered. For example to early-load the pytest-cov plugin you can use:

Disabling plugins¶

To disable loading specific plugins at invocation time, use the -p option together with the prefix no: .

Example: to disable loading the plugin doctest , which is responsible for executing doctest tests from text files, invoke pytest like this:

Other ways of calling pytest¶

Calling pytest through python -m pytest ¶

You can invoke testing through the Python interpreter from the command line:

This is almost equivalent to invoking the command line script pytest [. ] directly, except that calling via python will also add the current directory to sys.path .

Calling pytest from Python code¶

You can invoke pytest from Python code directly:

this acts as if you would call “pytest” from the command line. It will not raise SystemExit but return the exit code instead. If you don’t pass it any arguments, main reads the arguments from the command line arguments of the process ( sys.argv ), which may be undesirable. You can pass in options and arguments explicitly:

retcode = pytest.main(["-x", "mytestdir"]) 

You can specify additional plugins to pytest.main :

# content of myinvoke.py import sys import pytest class MyPlugin: def pytest_sessionfinish(self): print("*** test run reporting finishing") if __name__ == "__main__": sys.exit(pytest.main(["-qq"], plugins=[MyPlugin()])) 

Running it will show that MyPlugin was added and its hook was invoked:

$ python myinvoke.py *** test run reporting finishing

Calling pytest.main() will result in importing your tests and any modules that they import. Due to the caching mechanism of python’s import system, making subsequent calls to pytest.main() from the same process will not reflect changes to those files between the calls. For this reason, making multiple calls to pytest.main() from the same process (in order to re-run tests, for example) is not recommended.

Источник

Читайте также:  Java server many clients
Оцените статью