既然fixtures函数也是咱们自己写的,那难免会发生异常,当fixture函数异常后,pytest中如何处理呢?
首先,在pytest中,如果一个测试函数中传入了多个fixture函数,那么pytest会尽可能的按线性顺序先后执行。
如果,先执行的fixture函数有问题引发了异常,那么pytest将会停止执行这个测试函数的fixture,并且标记此测试函数有错误。
但是,当测试被标记为有错误时,并不是说这个测试函数的结果失败了,这仅仅意味着测试函数所依赖的fixture有问题,
导致测试函数不能正常进行。
所以,这就引出了另一个值得关注的点:fixture虽灵活好用,切记不要滥用。
在实际应用中,要尽可能的减少不必要的依赖关系。这样的话,测试函数就不会因为其他不相关的问题,导致自己不能正常运行。
结合代码示例,进一步了解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import pytest @pytest .fixture def order(): return [] @pytest .fixture def append_first(order): order.append( 1 ) @pytest .fixture def append_second(order, append_first): order.extend([ 2 ]) @pytest .fixture(autouse = True ) def append_third(order, append_second): order + = [ 3 ] def test_order(order): assert order = = [ 1 , 2 , 3 ] |
首先声明,这段代码是可以正常运行的,测试函数test_order也是正常通过的。
假设,不管怎样,在order.append(1)处总会报错。这时候,我们其实无法确定order.extend([2])或order +=[3]是否也有问题。
在append_first报错抛出异常后,pytest就不会继续运行任何的fixture函数了,就连测试函数test_order本身也不会运行。
以上就是pytest官方文档解读Fixture errors抛错解决的详细内容,更多关于pytest Fixture errors抛错的资料请关注服务器之家其它相关文章!
原文链接:https://www.cnblogs.com/pingguo-softwaretesting/p/14478614.html