08 Automatic Test Framework – 03PYTest Test framework

Why pyTest

The python testing frameworks are: UnnitTest, PyTest, and Nose.

Unittest is a standard unit testing framework provided by Python. Pytest is a better and easier testing framework based on UnitTest, with more plug-ins. And Pytest is compatible with almost all unitTest features, so we can use Unnitest features in Pytest and not vice versa.

Using pytest

  1. The installation

pip install pytest2. The structure test file name can not be the same, otherwise it will report an error, I write wrong here.A Test class must start with Test and cannot have an init method

As we can see from the above, what we need to know is

  • conftest
  • Run parameters of PyTest
  • Name of the pyTest use-case file

assertions

Assert condition, error output when the condition does not meet

# -*- coding: utf-8 -*- import pytest a=1 b=1 c=3 class TestDemo(): def test_01(self): Test def test_02(self): Assert a==b, 'a does not equal b' @pytest.mark.test def test_02(self): Assert a== C, 'a does not equal C 'if __name__ == '__main__': pytest.main(['-q', 'test_case_login.py'])Copy the code

Find the case

  1. Pytest directory

Pytest test_Case pytest.ini will run all tests starting with test_ or ending with _test in test_case.

[pytest]
testpaths=./test_cases
Copy the code
  1. To explicitly specify the function name, pass: :tag

Pytest test_case::test_01 py file: pytest.mian([‘test_case:: file name ‘])

  1. – Maxfail =1: The test is terminated if one failure occurs

  2. mark

We can label each function differently, and when we test that allows all functions to be labeled more than one function can be labeled; Multiple functions can also be marked the same.

Control operation parameters

  1. -s

By default, by default the print or log output that you write in the use case is not going to show up in the test results.

If you want to see standard output in a use case, add the -s argument

Pytest test_login.py -s pytest.main([' -s', 'test_login.py'])Copy the code
  1. -v

Using -v makes the output more detailed, with one file on one line when not in use and one use case after the use case on one line. And both the use case name and the result are displayed in the result, not just one. Or characters.

  1. -q

In contrast to -v, -q outputs more simplified information.

  1. -l

Using -l shows global and local variables during the run.

Parametrize Pytest.mark.parametrize (argnames, argvalues)

When a test function is tested, it is common to pass multiple sets of arguments to the function. pytest.mark.parametrize(argnames, argvalues)

  1. A single element

2. Multi-element parameters

In general, the data is separated from the logical layer, usually parsed from files such as Excel

The firmware fixtures

Pytest loads and runs test functions before (or after) they are executed. For example, many operations require us to log in, or prepare the database connection

In complex projects, confTest.py can be defined at different directory levels, scoped to its directory and subdirectories.

  1. pretreatment

Many times you need to do pre-processing before testing (such as creating a new database connection) and cleaning up after testing (closing the database connection).

When there is a large number of repeated such operations, it is best practice to use firmware to automate all pre-processing and post-processing.

Pytest uses yield keywords to split firmware into two parts. Code before yield is preprocessed and executed before test. Code after yield is post-processing and will be executed after the test is complete. Yield can be used to return the following parameters

  1. Scope of the scope
  • function: function level, each test function executes the firmware once;
  • class: class level, where each test class is executed once and all methods are available;
  • module: module-level, each module is executed once, and functions and methods in the module can be used;
  • session: session-level, where a test is executed only once and all found functions and methods are available.
  1. automated
autouse=True
Copy the code

pytest.ini

  1. Run test_01 of test_login

There are configuration files, and there are command lines.

  1. pytest.ini

In general, the common Settings for global configuration are as follows:

[pytest]

Addopts = – s… Multiple command line arguments can be added, separated by Spaces

Testpaths =./ test_Case # / PyTestProject is the pyTestProject folder of the previous level.

Python_files = test*.py# Sets the name of the module file to be searched for by the test

Python_classes = Test*# Configure the name of the Test class that the Test searches for

Python_funtions = test# Configures the test function name for the test search