首页 > 解决方案 > Pytest not able to register command line arguments

问题描述

This is my folder structure:

service
|
|--foolib
     |
     |--src
         |
         |--main
            |
            |--python3
                |
                |--foolib
                    |
                    |--conftest.py

and I run pytest from the root directory (service)

Here are the contents of conftest.py:

import pytest

def pytest_addoption(parser):
    parser.addoption("--dc", action="store", dest="dc")

def pytest_configure(config):
    DC = config.getoption("dc")
    #do something with DC

When I run pytest from the root directory:

==================================================================================================================== test session starts =====================================================================================================================
platform darwin -- Python 3.8.2, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/test/service
plugins: testrail-2.5.3
collected 0 items / 1 error                                                                                                                                                                                                                                  

=========================================================================================================================== ERRORS ===========================================================================================================================
_______________________________________________________________________________________________________________ ERROR collecting test session ________________________________________________________________________________________________________________
python3.8/site-packages/_pytest/config/__init__.py:1463: in getoption
    val = getattr(self.option, name)
E   AttributeError: 'Namespace' object has no attribute 'dc'
....

E   ValueError: no option named 'dc'
================================================================================================================== short test summary info ===================================================================================================================
ERROR  - ValueError: no option named 'dc'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
====================================================================================================================== 1 error in 0.15s ======================================================================================================================

How can I resolve this? Am I missing something in the conftest?

标签: pythonpytest

解决方案



推荐阅读