pytest knowledge learning

1, pytest installation

pip install -U pytest

2, view the installed version

pip show pytest

3, operated with cases

pytest operating rules: find the current directory and its subdirectories to test _ * py or * _test.py file, locate the file, find the line starting test function and execute the file.

example:

 Run steps:

(1) open cmd window

(2) into the project folder

(3) Enter the Run command

pytest file name

 Successful show. "" Failure to display "F"

 With -v, displays detailed information about the test

 

 <1> Run function name specified by marking ::

 

 <2> using a fuzzy matching operation with Example -k option identification

 <3>  After labeling function, the function is specified

4, pytest cases rules with

(1) test file begins test_ (ending _test can)

(2) Test to begin the test class, and not with __init__ method

(3) Test function to begin with test_

(4) assertions assert

5, skipping test

Skip embodiment with a desired operation, to be marked as skip on the use case

 

Test_func2 be skipped can be seen that, when executed, identified by s

 

 

 6, predictive error

We know in advance that will function tests failed, but do not want to skip, but want to be prompted.

 Use x identifies predictable failure

7, parameterization

 It can be seen performed three times

 8, pre-treatment and post-treatment

 Use -s represent prevent information from being swallowed

Unused -s:

Add running the -s, you can see a successful test before and after the connection identifier and closing operation of the database.

 9, fixture scope (the default scope for the function)

Fixture detached role is to facilitate repetitive work and reuse, for finer control fixture, pytest used to specify the scope of use of the fixture.

When defining fixture, using the scope parameter declarations scope, options are:

(1) function: function-level, the function is executed once for each test;

(2) class: class level is performed once for each test category, the methods can be used;

(3) module: module level, each module executed once, the function modules and methods can be used;

(4)session:会话级,一次测试只执行一次,所有被找到的函数和方法都可用。

 

 class 的使用

 pytest.mark.usefixtures 对函数和方法也适用

10、fixture 重命名

Fixture 的名称默认为定义时的函数名,如果不想使用默认,可以通过name选项指定名称:

 11、fixture 参数化

适用场景:一批API 需要测试不同数据库的支持情况(对所有数据库进行相同的操作)

 

附:Pytest 使用手册 — learning-pytest 1.0 文档

Guess you like

Origin www.cnblogs.com/yudx/p/12131895.html