我需要一个初始化变量的夹具(会话?),该变量将在测试中用于生成测试输出。我不需要在每个测试中都运行此固定装置,而仅在测试初始化期间运行。 该变量需要在测试中用于参数化。
在conftest.py中:
import os
import json
import glob
import csv
from gbl import *
def pytest_addoption(parser):
parser.addoption("--component", action="append", default=[], help="component to test")
def pytest_generate_tests(metafunc):
metafunc.parametrize("code", DTCLIST)
@pytest.fixture(scope="session", autouse=True)
def my_own_session_run_at_beginning(request):
print('\nIn my_own_session_run_at_beginning()')
GlobalDatabase = dict()
GlobalDataBaseName = os.path.join('c', 'myfolder', '10_Requirement', 'db.json')
with open(GlobalDataBaseName, 'r') as fp:
GlobalDatabase = json.load(fp)['codes']
component = request.config.getoption('component')[0]
diagCfg = list()
sources = ''
if component != 'all':
DTCFiles = glob.glob(os.path.join('c', 'myfolder', '*',
component, '20_SystemDesign', , '*.dtc'))
SrcFiles = glob.glob(os.path.join('c', 'myfolder', '*',
component, '40_SystemDesign', '*.c'))
else:
DTCFiles = glob.glob(os.path.join('c', 'myfolder', '*',
'*', '20_SystemDesign', '*.dtc'))
SrcFiles = glob.glob(os.path.join('c', 'myfolder', '*',
'*', '40_SystemDesign', '*.c'))
for DTCFile in DTCFiles:
with open(DTCFile, 'r') as csvFile:
csvLine = csv.reader(csvFile, delimiter=';')
for row in csvLine:
if row:
diagCfg.append({'code': row[0], 'SWName': row[1], 'fileName': DTCFile})
for SrcFile in SrcFiles:
with open(SrcFile, 'r') as fp:
for line in fp:
sources += line
GLOBALDB = GlobalDatabase
DTCLIST = diagCfg
SOURCESBUFFER = sources
gbl.py:
DTCLIST = list()
GLOBALDB = dict()
SOURCESBUFFER = ''
testdtc.py:
import re
import pytest
from gbl import *
def test_dtc_code_syntax(code):
assert re.match('\d+\.\d+\.\d+', code['code']), "{} does not have a proper syntax.".format(code['code'])
我希望函数my_own_session_run_at_beginning可以运行,但是在运行命令时:
pytest -v -s --component all testdtc.py
我陷入了跳过测试的情况
============================= test session starts =============================
platform win32 -- Python 2.7.0, pytest-3.3.0, py-1.5.2, pluggy-0.6.0 -- c:\cad\workspace\environment\python\2.7\python.exe
cachedir: .cache
metadata: {'Python': '2.7.0', 'Platform': 'Windows-post2008Server-6.2.9200', 'Packages': {'py': '1.5.2', 'pytest': '3.3.0', 'pluggy': '0.6.0'}, 'Plugins': {'rerunfailures': '3.1', 'tap': '2.1', 'json': '0.4.0', 'teamcity-messages': '1.17', 'html': '1.16.0', 'instafail': '0.3.0', 'metadata': '1.5.1'}}
rootdir: c:\git\diagchecker_, inifile:
plugins: teamcity-messages-1.17, tap-2.1, rerunfailures-3.1, metadata-1.5.1, json-0.4.0, instafail-0.3.0, html-1.16.0
collected 1 item
Test_Dtc.py::test_dtc_code_syntax[code0] SKIPPED [100%]
========================== 1 skipped in 0.04 seconds ==========================
有人对此有解决方案吗?
非常感谢您