pytest相关问题解析

1. 如果你想查询在你的环境下有哪些pytest的active plugin可以使用:

[plain] view plain copy
 
  1. 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。

 

function Run once per test
class Run once per class of tests
module Run once per module
session Run once per session

fixture又有autouse的和非autouse的。什么是autouse的呢?就是在定义fixture时autouse为True的,如下:

[plain] view plain copy
 
  1. @pytest.fixture(scope="session", autouse=True)  

在调用时pytest的test函数不需要在参数中指定它也会被调用,如下所示

[plain] view plain copy
 
  1. def test_alpha_1():  
  2.     print('\nIn test_alpha_1()')  

非autouse的fixture时没有指定autouse为True的。它在调用时需要显示地写出fixture的名字在pytest函数的参数中,如下:

[plain] view plain copy
 
  1. @pytest.fixture(scope="session")  
  2. def some_resource(request):  
  3.     print('\nIn some_resource()')  
  4.    
  5.     def some_resource_fin():  
  6.         print('\nIn some_resource_fin()')  
  7.     request.addfinalizer(some_resource_fin)  

在调用时需要这么调用

 

[plain] view plain copy
 
  1. def test_alpha_2(some_resource):  
  2.     print('\nIn test_alpha_2()')  

这两者在调用顺序上,在setup阶段,是先调用autouse的,然后再调用非autouse的。在tear down阶段则是反过来,先调用非autouse的,然后再调用autouse的。他们的调用顺序应该是类似于栈的进出栈顺序,先进栈的后出栈,后进栈的先出栈。

查看pytest自带的内在的fixture的方法。

[plain] view plain copy
 
  1. py.test -q --fixture  

pytest中有三种方法去使用一个fixture

  1. 在test的参数列表中指定。
  2. 使用usefixtures decorateor
  3. 使用autouse

4. 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 objects
  • Params
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
 
  1. path/to/testdir/pytest.ini  
  2. path/to/testdir/tox.ini  
  3. path/to/testdir/setup.cfg  
  4. path/to/pytest.ini  
  5. path/to/tox.ini  
  6. path/to/setup.cfg  
  7. ... # up until root of filesystem  

如果给py.test 提供参数,会从当前的工作路径开始寻找。

 

 

 转载'https://blog.csdn.net/xibeichengf/article/details/50589235

本文参考了这篇文章

posted @ 2018-03-29 16:42  公众号python学习开发  阅读(206)  评论(0编辑  收藏  举报