简体   繁体   中英

Use conftest.py fixtures in non-test function

Below is the Server.py file

class Server():
    def __init__(self, certral=None, user=None, password=None):
        cwd = os.path.dirname(__file__)
        self.propPath = cwd + "/../server-profiles.json"
        print(os.path.normpath(self.propPath))
        with open(os.path.normpath(self.propPath)) as data_file:
            data = json.load(data_file)

    self.propertyDataJson = data
        self.testcategory = data['generic']['category']

Below is the conftest.py file.

def pytest_addoption(parser):
    parser.addoption(
        "--stack", action="store", default="sanity", help="my option: sanity or qa"
    )


@pytest.fixture
def cmdopt(request):
    return request.config.getoption("--stack")

Now, I want the user to give input pytest xyz.py --stack=sanity and the value should be stored in self.testcategory , for ex: self.testcategory = sanity or self.testcategory = qa under Server() class in Server.py file.

I am not sure what you are trying to accomplish with this but if I have to guess:

I think this can only be done if you create a server fixture:

@pytest.fixture
def server(cmdopt):
    server_ = Server()
    server_.testcategory = cmdopt
    yield server_

Or if that is not an option for you just add the flag in the test function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM