我正在使用 pytest 编写一些测试,它需要一个环境变量来运行测试函数。所以我创建了一个固定装置来检查环境变量是否存在或退出。下面是我的代码。
import os
import sys
import pytest
from ..factorial import fact
@pytest.fixture(scope='class')
def pre(request):
print('************setting up************')
pwd = os.environ.get('PASSWD', None)
if pwd is not None:
request.cls.pwd = pwd
else:
sys.exit('This test requires your password, '
'please run export PASSWD=<yourPassword>')
@pytest.mark.usefixtures('pre')
class TestFactorial:
def test_postive(self):
assert fact(5) == 120
def test_false(self):
assert fact(6) == 720
设置环境变量后,我得到如下预期输出,fixture 实际上每个类运行一次。
collected 2 items
test_factorial.py ************setting up************
..
============================================================================================= 2 passed in 0.01 seconds ===
现在当没有设置环境变量时,整个 fixture 会运行两次。下面是输出
collected 2 items
test_factorial.py ************setting up************
E************setting up************
E
====================================================================================================== ERRORS ======================================================================================================
___________________________________________________________________________________ ERROR at setup of TestFactorial.test_postive ___________________________________________________________________________________
request = <SubRequest 'pre' for <Function test_postive>>
@pytest.fixture(scope='class')
def pre(request):
print('************setting up************')
pwd = os.environ.get('PASSWD', None)
if pwd is not None:
request.cls.pwd = pwd
else:
> sys.exit('This test requires your password, '
'please run export PASSWD=<yourPassword>')
E SystemExit: This test requires your password, please run export PASSWD=<yourPassword>
test_factorial.py:17: SystemExit
____________________________________________________________________________________ ERROR at setup of TestFactorial.test_false ____________________________________________________________________________________
request = <SubRequest 'pre' for <Function test_false>>
@pytest.fixture(scope='class')
def pre(request):
print('************setting up************')
pwd = os.environ.get('PASSWD', None)
if pwd is not None:
request.cls.pwd = pwd
else:
> sys.exit('This test requires your password, '
'please run export PASSWD=<yourPassword>')
E SystemExit: This test requires your password, please run export PASSWD=<yourPassword>
test_factorial.py:17: SystemExit
我原以为会引发异常并且程序会退出并且它甚至不会到达 TestFactorial 类。但这表明 fixture 实际上在类中的每个测试方法中运行了两次。
这是预期的行为还是我在理解固定装置的实际工作方式时遗漏了什么?
如果这确实是预期的,那么我如何才能实现这种行为,即如果未设置环境变量,则不应运行测试?
编辑 1
我对我的代码进行了更多更改,以实际查看如果 fixture 中存在异常并且确实如此, fixture 是否为每个方法运行。下面是更新后的代码和结果
import os
import sys
from random import randint # edit
import pytest
from ..factorial import fact
@pytest.fixture(scope='class')
def pre(request):
print('************setting up************')
pwd = os.environ.get('PASSWD', None)
if pwd is not None:
print(randint(1, 100)) # edit
request.cls.pwd = randint(1, 100)
else:
print(randint(1, 100)) # edit
sys.exit('This test requires your password, '
'please run export PASSWD=<yourPassword>')
@pytest.mark.usefixtures('pre')
class TestFactorial:
def test_postive(self):
assert fact(5) == 120
def test_false(self):
assert fact(6) == 720
设置环境变量时的输出
pytest -sq test_factorial.py
************setting up************
67
..
2 passed in 0.01 seconds
所以它只为类(class)跑了一次。
未设置环境变量时的输出
pytest -sq test_factorial.py
************setting up************
69
E************setting up************
82
E
====================================================================================================== ERRORS ======================================================================================================
___________________________________________________________________________________ ERROR at setup of TestFactorial.test_postive ___________________________________________________________________________________
request = <SubRequest 'pre' for <Function test_postive>>
@pytest.fixture(scope='class')
def pre(request):
print('************setting up************')
pwd = os.environ.get('PASSWD', None)
if pwd is not None:
print(randint(1, 100))
request.cls.pwd = randint(1, 100)
else:
print(randint(1, 100))
> sys.exit('This test requires your password, '
'please run export PASSWD=<yourPassword>')
E SystemExit: This test requires your password, please run export PASSWD=<yourPassword>
test_factorial.py:20: SystemExit
____________________________________________________________________________________ ERROR at setup of TestFactorial.test_false ____________________________________________________________________________________
request = <SubRequest 'pre' for <Function test_false>>
@pytest.fixture(scope='class')
def pre(request):
print('************setting up************')
pwd = os.environ.get('PASSWD', None)
if pwd is not None:
print(randint(1, 100))
request.cls.pwd = randint(1, 100)
else:
print(randint(1, 100))
> sys.exit('This test requires your password, '
'please run export PASSWD=<yourPassword>')
E SystemExit: This test requires your password, please run export PASSWD=<yourPassword>
test_factorial.py:20: SystemExit
2 error in 0.03 seconds
请注意,每种情况下打印的随机数都不同,因此这表明 fixture 实际上触发了两次,针对类中的每个方法一次。
这是非常奇怪的行为。有什么线索吗?
请您参考如下方法:
pytest 捕获 SystemExit 异常,当您考虑它时这是合理的:如果您正在测试碰巧调用 sys.exit 的函数,您不会不希望这关闭您的整个测试套件。
使用 pytest.exit 而不是 sys.exit,这会向 pytest 发出信号,表明您想要真正关闭测试套件。
