[PYTHON] Pre-processing and post-processing of pytest

Pre-processing and post-processing of pytest

For each test item, test class, and entire test module (py file) with pytest How to write to perform pre-processing and post-processing.

Summary

Functions / methods to be used as targets for pre-processing / post-processing

--Each test function (test_ ~~) --setup_function (function) and teardown_function (function) --Each test method of the test class (test_ ~~ in the class) --setup_method (self, method) and teardown_method (self, method) --Whole test class (class Test ~~) --setup_class (cls) and teardown_class (cls)

Sample (when writing with xunit)

When writing with xunit


def setup_module(module):
    print("\n*** setup_module ***")

def teardown_module(moduloe):
    print("\n*** teardown_module ***")

def setup_function(function):
    print("\n=== setup_function ===")

def teardown_function(function):
    print("\n=== teardown_function ===")

def test_test1():
    print("test1")

def test_test2():
    print("test2")


class TestCase:
    @classmethod
    def setup_class(cls):
        print("\n@@@ setup_class @@@")

    @classmethod
    def teardown_class(cls):
        print("\n@@@ teardown_class @@@")

    def setup_method(self, method):
        print("\n--- setup_method ---")

    def teardown_method(self, method):
        print("\n--- teardown_method ---")

    def test_test3(self):
        print("test3")

    def test_test4(self):
        print("test4")

Execution result

Execution result


==================================== test session starts ====================================
platform linux -- Python 3.6.3, pytest-5.3.0, py-1.8.0, pluggy-0.13.1 -- /home/bskke1040/.pyenv/versions/3.6.3/bin/python3.6
cachedir: .pytest_cache
rootdir: /mnt/c/Users/bskke1040.KMJP/Clouds/OneDrive/study/pytest
collected 4 items

test_sample.py::test_test1
*** setup_module ***

=== setup_function ===
test1
PASSED
=== teardown_function ===

test_sample.py::test_test2
=== setup_function ===
test2
PASSED
=== teardown_function ===

test_sample.py::TestCase::test_test3
@@@ setup_class @@@

--- setup_method ---
test3
PASSED
--- teardown_method ---

test_sample.py::TestCase::test_test4
--- setup_method ---
test4
PASSED
--- teardown_method ---

@@@ teardown_class @@@

*** teardown_module ***


===================================== 4 passed in 0.05s =====================================

Sample (when writing with fixture)

When writing with fixture


import pytest


@pytest.fixture(scope="module")
def my_setup_module(request):
    print("\n######## my_setup_module ########")

    def my_teardown_module():
        print("\n######## my_teardown_module ########")
    request.addfinalizer(my_teardown_module)


@pytest.fixture()
def my_setup_function(request):
    print("\n--- my_setup_function ---")

    def my_teardown_function():
        print("\n--- my_teardown_function ---")
    request.addfinalizer(my_teardown_function)


@pytest.fixture(scope="class")
def my_setup_class(request):
    print("\n***** my_setup_class *****")

    def my_teardown_class():
        print("\n***** my_teardown_class *****")
    request.addfinalizer(my_teardown_class)


@pytest.fixture()
def my_setup_method(request):
    print("\n=== my_setup_method ===")

    def my_teardown_method():
        print("\n=== my_teardown_method ===")
    request.addfinalizer(my_teardown_method)


def test_test1(my_setup_module, my_setup_function):
    print("\ntest1")


def test_test2(my_setup_module, my_setup_function):
    print("\ntest2")


class TestCase:
    def test_test3(self, my_setup_module, my_setup_class, my_setup_method):
        print("\ntest3")

        def test_test4(self, my_setup_module, my_setup_class, my_setup_method):
            print("\ntest4")

Recommended Posts

Pre-processing and post-processing of pytest
Installation and easy usage of pytest
Data cleansing 3 Use of OpenCV and preprocessing of image data
Preprocessing of prefecture data
Overview of natural language processing and its data preprocessing
Problems of liars and honesty
Mechanism of pyenv and virtualenv
Super basic usage of pytest
Types of preprocessing in natural language processing and their power
Combination of recursion and generator
Combination of anyenv and direnv
Explanation and implementation of SocialFoceModel
Python application: Data cleansing # 3: Use of OpenCV and preprocessing of image data
Differentiation of sort and generalization of sort
Coexistence of pyenv and autojump
Use and integration of "Shodan"
Problems of liars and honesty
Occurrence and resolution of tensorflow.python.framework.errors_impl.FailedPreconditionError
Comparison of Apex and Lamvery
Source installation and installation of Python
Introduction and tips of mlflow.Tracking
Environment construction of python and opencv
Various of Tweepy. Ma ♡ and ♡ me ♡
Basic knowledge of Linux and basic commands
Order of arguments of RegularGridInterpolator and interp2d
The story of Python and the story of NaN
Explanation and implementation of PRML Chapter 4
Introduction and Implementation of JoCoR-Loss (CVPR2020)
Benefits and examples of using RabbitMq
Explanation and implementation of ESIM algorithm
Danger of mixing! ndarray and matrix
Installation of SciPy and matplotlib (Python)
Significance of machine learning and mini-batch learning
Introduction and implementation of activation function
Memorandum of saving and loading model
Scraping, preprocessing and writing to postgreSQL
Misunderstandings and interpretations of Luigi's dependencies
Explanation and implementation of simple perceptron
Calculation of homebrew class and existing class
This and that of python properties
Design of experiments and combinatorial optimization
Clash of Clans and image analysis (3)
Time series analysis 3 Preprocessing of time series data
Features of symbolic and hard links
Coexistence of Python2 and 3 with CircleCI (1.0)
Summary of Python indexes and slices
Aggregation and visualization of accumulated numbers
Reputation of Python books and reference books
Preprocessing of Wikipedia dump files and word-separation of large amounts of data by MeCab
Python: Preprocessing in machine learning: Handling of missing, outlier, and imbalanced data