joh*_*hnw 2 python automated-tests pytest
我正在使用Pytest测试可执行文件。该.exe文件在启动时读取配置文件。
我编写了一个夹具,以在每次测试开始时生成此.exe文件,并在测试结束时将其关闭。但是,我无法解决如何告诉灯具使用哪个配置文件的问题。我希望固定装置在生成.exe文件之前将指定的配置文件复制到目录中。
@pytest.fixture
def session(request):
copy_config_file(specific_file) # how do I specify the file to use?
link = spawn_exe()
def fin():
close_down_exe()
return link
# needs to use config file foo.xml
def test_1(session):
session.talk_to_exe()
# needs to use config file bar.xml
def test_2(session):
session.talk_to_exe()
Run Code Online (Sandbox Code Playgroud)
我如何告诉灯具使用foo.xml的test_1功能和bar.xml用于test_2功能?
谢谢约翰
一种解决方案是pytest.mark用于:
import pytest
@pytest.fixture
def session(request):
m = request.node.get_closest_marker('session_config')
if m is None:
pytest.fail('please use "session_config" marker')
specific_file = m.args[0]
copy_config_file(specific_file)
link = spawn_exe()
yield link
close_down_exe(link)
@pytest.mark.session_config("foo.xml")
def test_1(session):
session.talk_to_exe()
@pytest.mark.session_config("bar.xml")
def test_2(session):
session.talk_to_exe()
Run Code Online (Sandbox Code Playgroud)
另一种方法是仅session稍微更改固定装置,以将链接的创建委派给测试功能:
import pytest
@pytest.fixture
def session_factory(request):
links = []
def make_link(specific_file):
copy_config_file(specific_file)
link = spawn_exe()
links.append(link)
return link
yield make_link
for link in links:
close_down_exe(link)
def test_1(session_factory):
session = session_factory('foo.xml')
session.talk_to_exe()
def test_2(session):
session = session_factory('bar.xml')
session.talk_to_exe()
Run Code Online (Sandbox Code Playgroud)
我更喜欢后者,因为它更易于理解,并且可以在以后进行更多改进,例如,如果您需要@parametrize在基于config值的测试中使用。还要注意,后者允许在同一测试中产生多个可执行文件。
| 归档时间: |
|
| 查看次数: |
574 次 |
| 最近记录: |