Pytest的配置文件(八)

      在前面的文章中介绍了Pytest中使用conftest.py来共享fixture,事实上我们可以通过Pytest中的配置文件pytest.ini

来改变Pytest的执行方式,如指定pytest执行的最低版本,明确规定什么目录下的测试点是不可以执行的,那些目录

下的测试点是可以执行的,以及执行过程中它的搜索规则,我们可以依据自己的需求来进行自定义。下面就依据这些来

分别介绍这些的应用。不得不说,Pytest真的很自由,没有unittest那么多的拘束,这点比较自由。

一、指定命令行选项

      在执行中,特别是Pytest的命令行执行中,经常会指定命令行后面的参数,如-v的参数会显示详细的信息,关于

这些参数在这里就不再详细的介绍。在执行Pyest后,我们更多的是想看到所有的测试点都被执行,所以要应用Pytest

的配置文件,首先在一个项目中创建pytest.ini的配置文件,如下面的配置文件指定了执行所有测试点,并且详细的信息

输出,见配置文件内容:

[pytest]
addopts= -v

 

见要执行的测试点,在src和tests包各自存在一个测试模块,目录截图:

 

 

 见执行pytest命令后输出的信息,特别说明这里我们执行的时候就不需要指定pytest -v了,直接pytest就可以看到详细的信息,如

下图所示:

collecting ... 
 src/test_src.py::test_001 ✓                                      33% ███▍      
 src/test_src.py::test_002 ✓                                      67% ██████▋   
 tests/test_ini.py::test_passing ✓                               100% ██████████

Results (0.04s):
       3 passed

但是很多的时候,我们希望看到的是简化的信息,那么配置文件可以修改为:

[pytest]
addopts= -rsxX -l --tb=short

 

见执行后输出的信息:

collecting ... 
 src/test_src.py ✓✓                                               67% ██████▋   
 tests/test_ini.py ✓                                             100% ██████████

Results (0.03s):
       3 passed

 

二、注册标记

     在编写测试点的时候,由于业务的需要,或者说我们会依据产品模块的分类,编写的测试点会进行分类,所以也希望执行的时候

某些分类是可以执行的,但是没有写在配置文件里面的分类是不可以执行的,如最新的配置文件为:

[pytest]
addopts= -v -rsxX -l --tb=short --strict
markers
= smoke:执行标记为smoke的测试函数 login:执行标记为login的测试函数

 

依据上面的配置文件信息,可以得出只执行标记为smoke和login的测试点,而其他的不会被执行,并且打印详细的信息,见要执行的

测试模块的源码:

#!/usr/bin/python3
#coding:utf-8

import  pytest

def test_passing():
    assert  1==1

@pytest.mark.smoke
def test_smoke_001():
    pass

@pytest.mark.smoke
def test_smoke_002():
    pass


@pytest.mark.login
def test_login_001():
    pass

@pytest.mark.login
def test_login_002():
    pass

@pytest.mark.logout
def test_logout_001():
    pass

@pytest.mark.logout
def test_logout_002():
    pass

 

执行后,会显示出未注册的标记,见输出的信息:

collecting ... 
―――――――――――――――――――――― ERROR collecting tests/test_ini.py ――――――――――――――――――――――
'logout' not a registered marker

!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!

Results (0.09s):

 

可以看到错误的提示信息提示我们logou未注册,那么解决的办法要么是注册,要么是注释这些测试代码,其实也可以通过

pytesyt --markers来查看注册的标记,见执行后输出的信息:

@pytest.mark.smoke:执行标记为smoke的测试函数

@pytest.mark.login:执行标记为login的测试函数

@pytest.mark.no_cover: disable coverage for this test.

@pytest.mark.filterwarnings(warning): add a warning filter to the given test. see https://docs.pytest.org/en/latest/warnings.html#pytest-mark-filterwarnings 

@pytest.mark.skip(reason=None): skip the given test function with an optional reason. Example: skip(reason="no way of currently testing this") skips the test.

@pytest.mark.skipif(condition): skip the given test function if eval(condition) results in a True value.  Evaluation happens within the module global context. Example: skipif('sys.platform == "win32"') skips the test if we are on the win32 platform. see https://docs.pytest.org/en/latest/skipping.html

@pytest.mark.xfail(condition, reason=None, run=True, raises=None, strict=False): mark the test function as an expected failure if eval(condition) has a True value. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. If only specific exception(s) are expected, you can list them in raises, and if the test fails in other ways, it will be reported as a true failure. See https://docs.pytest.org/en/latest/skipping.html

@pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in different arguments in turn. argvalues generally needs to be a list of values if argnames specifies only one name or a list of tuples of values if argnames specifies multiple names. Example: @parametrize('arg1', [1,2]) would lead to two calls of the decorated test function, one with arg1=1 and another with arg1=2.see https://docs.pytest.org/en/latest/parametrize.html for more info and examples.

@pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see https://docs.pytest.org/en/latest/fixture.html#usefixtures 

@pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible.

@pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible.

那么就在配置文件中增加logout的标记信息,再执行,见输出的结果信息:

collecting ... 
 src/test_src.py::test_001 ✓                                      11% █▎        
 src/test_src.py::test_002 ✓                                      22% ██▎       
 tests/test_ini.py::test_passing ✓                                33% ███▍      
 tests/test_ini.py::test_smoke_001 ✓                              44% ████▌     
 tests/test_ini.py::test_smoke_002 ✓                              56% █████▋    
 tests/test_ini.py::test_login_001 ✓                              67% ██████▋   
 tests/test_ini.py::test_login_002 ✓                              78% ███████▊  
 tests/test_ini.py::test_logout_001 ✓                             89% ████████▉ 
 tests/test_ini.py::test_logout_002 ✓                            100% ██████████

Results (0.04s):
       9 passed

 

三、指定pytest的版本号

     Pytest的版本目前比较多,那么到底是按那本版本执行了?另外并不是所有的执行都是在本机执行的,很多时候本终端编写的

代码而执行是在另外一个终端,那么就需要指定最低执行的Pytest的版本,配置文件新增为:

[pytest]
addopts= -v -rsxX -l --tb=short --strict

markers=
  smoke:执行标记为smoke的测试函数
  login:执行标记为login的测试函数
  logout:执行标记为logout的测试函数

#指定pytest的最低版本号
minversion=4.0

 

目前我的终端版本是4.0.2,如果我指定最低执行版本号为4.0.3,那么就无法执行,那么就会提示如下的错误信息:

pytest.ini:10: requires pytest-4.0.3, actual pytest-4.0.2'

所以对这点要特别的注意,如果指定的最低版本实际高于要执行的终端,导致的结果是无法执行。

四、忽略执行某些目录

      并不是所有目录下的测试点都需要被执行,比如src包下的测试模块并不想被执行,只想执行tests包下的测试模块,那么

就需要在配置文件中使用关键字norecurse来明确指定要忽略执行的目录,见完善后的配置文件:

[pytest]
addopts= -rsxX -l --tb=short --strict

markers=
  smoke:执行标记为smoke的测试函数
  login:执行标记为login的测试函数
  logout:执行标记为logout的测试函数

#指定pytest的最低版本号
minversion=4.0

#忽略执行src包下的测试模块中的test case
norecursedirs=src

 

执行后就不会执行src包下的测试模块中的测试点,见执行后输出的信息:

collecting ... 
 tests/test_ini.py ✓✓✓✓✓✓✓                                       100% ██████████

Results (0.03s):
       7 passed

 

五、指定要执行的目录

       testpaths与norecurse刚好相反,指定了那些目录需要被执行,如指定执行tests目录下的测试点,见完善

后的配置文件内容:

[pytest]
addopts= -rsxX -l --tb=short --strict

markers=
  smoke:执行标记为smoke的测试函数
  login:执行标记为login的测试函数
  logout:执行标记为logout的测试函数

#指定pytest的最低版本号
minversion=4.0

;#忽略执行src包下的测试模块中的test case
;norecursedirs=src

#指定测试目录
testpaths=tests

 

依据上面的配置文件可以看到,只执行tests目录下的测试模块(当然也包含了包),执行后只执行tests目录下的测试点。

      谢谢您的阅读和关注,如您感兴趣,也可关注我的公众号和购买实战课程,谢谢!!!

 

 

 

 

      

    

 

 

 

posted @ 2019-10-07 22:02  无涯(WuYa)  阅读(576)  评论(0编辑  收藏  举报