我正在编写我的集成测试代码,然后我有一个父
BaseTestClass
类要扩展到儿童类。父基类应该从资源yaml
文件中读取动态属性,以便于更改环境等。要使用的测试框架是pytest
.
一旦我执行下面的代码,就会在我解析参数的行中触发错误:
args = parser.parse_args()
。我找出错误的线索已经结束了
test_base.py
import yaml
import pytest
import argparse
class BaseTestClass:
@classmethod
def configure(cls):
print('Configure')
# Read env from CLI args
parser = argparse.ArgumentParser()
parser.add_argument('--env',
choices=['local', 'dev', 'qa', 'prod'],
help='The execution environment',
default='local')
args = parser.parse_args() # Code is breaking here.
env = args.env
with open(f"resources/config-{env}.yaml", 'r') as file:
properties = yaml.safe_load(file)
cls.config = properties
@pytest.fixture(autouse=True)
def configure_parent(self):
self.configure()
错误日志:
usage: _jb_pytest_runner.py [-h] [--env {local,dev,qa,prod}]
_jb_pytest_runner.py: error: unrecognized arguments: --json=report.json /Users/agstcadini/Projects/quinta/quinta-app/python/quinta-backend/tests/integration
test setup failed
self = <test_api.TestApi object at 0x1088bd640>
@pytest.fixture(autouse=True)
def configure_parent(self):
> self.configure()
test_base.py:24:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
test_base.py:16: in configure
args = parser.parse_args()
/usr/local/Cellar/[email protected]/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/argparse.py:1827: in parse_args
self.error(msg % ' '.join(argv))
/usr/local/Cellar/[email protected]/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/argparse.py:2581: in error
self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = ArgumentParser(prog='_jb_pytest_runner.py', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)
status = 2
message = '_jb_pytest_runner.py: error: unrecognized arguments: --json=report.json /Users/agstcadini/Projects/quinta/quinta-app/python/quinta-backend/tests/integration\n'
def exit(self, status=0, message=None):
if message:
self._print_message(message, _sys.stderr)
> _sys.exit(status)
E SystemExit: 2
/usr/local/Cellar/[email protected]/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/argparse.py:2568: SystemExit
文件系统
% tree -v -L 2
.
├── __pycache__
│ ├── test_api.cpython-39-pytest-7.3.1.pyc
│ └── test_base.cpython-39-pytest-7.3.1.pyc
├── report.json
├── resources
│ ├── config-dev.yaml
│ └── config-local.yaml
├── test_api.py
└── test_base.py
config-local.yaml
endpoint:
host: localhost
port: 8081