1. 如果你想查询在你的环境下有哪些pytest的active plugin可以使用:
[plain] view plain copy py.test --traceconfig会得到一个扩展的头文件名显示激活的插件和他们的名字。同时也会打印出当前的plugin,也就是被加载时conftest.py文件。
2. pytest.ini文件有什么作用
3. pytest的fixture究竟是怎么工作的,在pytest中它有怎样的作用。
Dealing with fixtures is one of the areas where pytest really shines.
It's to think of fixtures as a set of resources that need to be set up before a test starts, and cleaned up after.
有四种作用域的fixture,分别为function,class,module和session作用域。简单理解function scope,就是每一个函数都会调用;class scope,就是每一个类调用一次,一个类可以有多个函数;module scope,应该是一个文件调用一次,该文件内又有多个function;session scope范围更大,是多个文件调用一次,每个文件有对应着一个module。
functionRun once per testclassRun once per class of testsmoduleRun once per modulesessionRun once per session
fixture又有autouse的和非autouse的。什么是autouse的呢?就是在定义fixture时autouse为True的,如下:
[plain] view plain copy @pytest.fixture(scope="session", autouse=True)在调用时pytest的test函数不需要在参数中指定它也会被调用,如下所示
[plain] view plain copy def test_alpha_1(): print('\nIn test_alpha_1()')非autouse的fixture时没有指定autouse为True的。它在调用时需要显示地写出fixture的名字在pytest函数的参数中,如下:
[plain] view plain copy @pytest.fixture(scope="session") def some_resource(request): print('\nIn some_resource()') def some_resource_fin(): print('\nIn some_resource_fin()') request.addfinalizer(some_resource_fin)在调用时需要这么调用
[plain] view plain copy def test_alpha_2(some_resource): print('\nIn test_alpha_2()')
这两者在调用顺序上,在setup阶段,是先调用autouse的,然后再调用非autouse的。在tear down阶段则是反过来,先调用非autouse的,然后再调用autouse的。他们的调用顺序应该是类似于栈的进出栈顺序,先进栈的后出栈,后进栈的先出栈。
查看pytest自带的内在的fixture的方法。
[plain] view plain copy py.test -q --fixturepytest中有三种方法去使用一个fixture
在test的参数列表中指定。使用usefixtures decorateor使用autouse4. pytest fixture的一些优势
很直观明了地知道哪些tests使用了一个资源,因为这个资源在test的参数列表中。我不用必须 人为地创建类(或者将tests从一个文件移动到另一个),只需要分离fixture应用。对于一个资源来说teardown代码是紧密地和setup代码耦合的。资源的生命周期的范围在资源setup代码的位置处指定。这最终成为一个巨大的优势当你想要摆弄范围来节省测试时间。如果所有的事情开始出现故障,只需要一行的改变去指定函数的范围,让setup/teardown运行围绕着每一个函数/方法更少的代码。pytest的解决方法小于类的解决方法。 因为翻译水平有限,为了不影响英文愿意,我把英文也附在这里 It's obvious which tests are using a resource, as the resource is listed in the test param list.I don't have to artificially create classes (or move tests from one file to another) just to separate fixture usage.The teardown code is tightly coupled with the setup code for one resource.Scope for the lifetime of the resource is specified at the location of the resource setup code. This ends up being a huge benefit when you want to fiddle with scope to save time on testing. If everything starts going haywire, it's a one line change to specify function scope, and have setup/teardown run around every function/method.It's less code. The pytest solution is smaller than the class solution.5.pytest fixture的一些特性
Return value 你可以返回任何你想要返回的从fixture函数。 如果你的fixture是准备一些data,或者读取一个文件,或者打开一个到数据库的连接,然后访问数据库或资源应该是你想要从fixture中返回的。 Finalizer is teardown 当所有的test case使用了fixture后finalizer被调用 Request objectsParams fixture decorator的一个可选的参数是‘params’,它的默认值为None. 在params中的每一个值,该值会赋值给request.param,然后fixture将会被调用。
6. conftest.py的作用
conftest.py文件是一个单独的存放fixtures的文件。
对于function,class和module来说,把fixture代码放到和test代码同样的文件中完全合理的。但是,对于session,就不再make senese了。
这样的话我们就可以把他们放在conftest.py文件中。这是一个pytest会寻找的一个有特定名字的文件。
7. ini文件查找顺序
pytest默认的ini文件查找顺序为:pytest.ini, tox.ini, setup.cfg。只到第一个[pytest]部分被发现。
例如当我们执行:py.test path/to/testdir时,查找的顺序如下:
[plain] view plain copy path/to/testdir/pytest.ini path/to/testdir/tox.ini path/to/testdir/setup.cfg path/to/pytest.ini path/to/tox.ini path/to/setup.cfg ... # up until root of filesystem
如果给py.test 提供参数,会从当前的工作路径开始寻找。
转载'https://blog.csdn.net/xibeichengf/article/details/50589235
本文参考了这篇文章
转载于:https://www.cnblogs.com/c-x-a/p/8670784.html
相关资源:通过代码实例解析Pytest运行流程