pytest寻找命令行参数的配置文件顺序是:pytest.ini,tox.ini和setup.cfg。
例如当我们执行:
- py.test path/to/testdir path/other/
py.test path/to/testdir path/other/
的时候,pytest将会从所有测试目录的共同目录下开始寻找配置文件,直到寻找到系统的根目录位置。具体的是:
- # 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
# 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进入的目录,我们可以将这些目录或者文件写入到配置文件中,例如:
- # content of setup.cfg
- [pytest]
- norecursedirs = .svn _build tmp*
# content of setup.cfg [pytest] norecursedirs = .svn _build tmp*
这样,pytest就不会进入到.svn、_build、和任何tmp开头的文件中去收集测试函数。
4、指定什么样的类被认为是测试类
默认情况下Test开头的类被认为是测试类,pytest将其中的所有test开头的方法认为是测试方法。我们也可以指定什么样的类被认为是测试类,例如指定Suite结尾的类是测试类:
- # content of pytest.ini
- [pytest]
- python_classes = *Suite
# content of pytest.ini [pytest] python_classes = *Suite
5、指定什么样的文件被认为是测试文件
默认情况下,以test开头或者结尾的文件被认定为测试文件,pytest将在其中寻找测试方法。我们也可以指定什么样的文件被认为是测试文件,例如指定_file结尾的文件是测试文件:
- # content of pytest.ini
- [pytest]
- python_classes = *_file
# content of pytest.ini [pytest] python_classes = *_file
6、指定什么样的函数被认为测试函数
默认情况下,所有以test开头的函数都被认为是测试函数。我们可以指定什么样的函数是测试函数,例如,指定_test结尾的函数是测试函数:
- # content of pytest.ini
- [pytest]
- python_functions = *_test