pytest之 配置文件

pytest寻找命令行参数的配置文件顺序是:pytest.ini,tox.ini和setup.cfg。

例如当我们执行:

 

  1. py.test path/to/testdir path/other/  
py.test path/to/testdir path/other/

的时候,pytest将会从所有测试目录的共同目录下开始寻找配置文件,直到寻找到系统的根目录位置。具体的是:

 

  1. # first look for pytest.ini files  
  2. path/pytest.ini  
  3. path/setup.cfg     # must also contain [pytest] section to match  
  4. path/tox.ini       # must also contain [pytest] section to match  
  5. pytest.ini  
  6. ...                # all the way down to the root  
# first look for pytest.ini files
path/pytest.ini
path/setup.cfg     # must also contain [pytest] section to match
path/tox.ini       # must also contain [pytest] section to match
pytest.ini
...                # all the way down to the root

为了让pytest更快的找到配置文件,我们最好是将其放到所有测试脚本的顶层目录下,如上例中的path目录下。

3、如何避免pytest寻找某些目录或者文件中的测试函数

默认的情况下,pytest将会进入到当前目录下的目录和文件中,去收集测试用例(test_开头的函数)。但是这几个目录和文件pytest是不会进入的:

 

  •  ’.*’:以‘.’开头的文件和目录
  • ’CVS’:CVS文件
  • ’_darcs’:_darcs版本控制目录中
  • ’{arch}’:
  • ’*.egg’:所有.egg结尾的文件中。

 

我们可以自定义不让pytest进入的目录,我们可以将这些目录或者文件写入到配置文件中,例如:

 

  1. # content of setup.cfg  
  2. [pytest]  
  3. norecursedirs = .svn _build tmp*  
# content of setup.cfg
[pytest]
norecursedirs = .svn _build tmp*

这样,pytest就不会进入到.svn、_build、和任何tmp开头的文件中去收集测试函数。

4、指定什么样的类被认为是测试类

默认情况下Test开头的类被认为是测试类,pytest将其中的所有test开头的方法认为是测试方法。我们也可以指定什么样的类被认为是测试类,例如指定Suite结尾的类是测试类:

  1. # content of pytest.ini  
  2. [pytest]  
  3. python_classes = *Suite  
# content of pytest.ini
[pytest]
python_classes = *Suite

 

5、指定什么样的文件被认为是测试文件

默认情况下,以test开头或者结尾的文件被认定为测试文件,pytest将在其中寻找测试方法。我们也可以指定什么样的文件被认为是测试文件,例如指定_file结尾的文件是测试文件:

 

  1. # content of pytest.ini  
  2. [pytest]  
  3. python_classes = *_file  
# content of pytest.ini
[pytest]
python_classes = *_file

 

6、指定什么样的函数被认为测试函数

 

默认情况下,所有以test开头的函数都被认为是测试函数。我们可以指定什么样的函数是测试函数,例如,指定_test结尾的函数是测试函数:

  1. # content of pytest.ini  
  2. [pytest]  
  3. python_functions = *_test  
posted @ 2017-06-15 13:36  金竹多多  Views(1529)  Comments(0Edit  收藏  举报