Why custom values

We have multiple test environments, we call them dev1, dev2, devN, and our automated test switching environment is to modify the parameters in the properties file of the code to determine which environment to run the case on. The boss thought this was really inconvenient, so one day he slapped his thigh and said, “Damn, why don’t we just pass variables in the pyTest way so we don’t have to switch back and forth in the code ourselves?” Hence the following series of studies…

How to implement

Let’s take a look at some code

The first paragraph is the test case

#test_xxx.py
import pytest
import os
import sys


from atframework import *

class TestBoCheck:
    webflow = FlowsAPI()
    resourcemaps = ResourceMaps()
    utils = Utils()

    def setup_class(self) :
        logger.info(
            "[AtLog] ----- START of Test suit test_bo_search_player ----- ")

    def teardown_class(self) :
        self.webflow.close_browser()
        logger.info(
            "[AtLog] ----- END of Test suit test_bo_search_player ----- ")

    def test_bo_check(self, setup_opts) :
        self.utils.set_setup_opts(setup_opts)
        ...
Copy the code

The second paragraph is the confTest file set up.

#conftest.py
import pytest


def pytest_addoption(parser) :
    parser.addoption(
        "--browser", action="store", default="".help="option: chrome, safari, firefox"
    )
    parser.addoption(
        "--site", action="store", default="".help="option: dev, stage, local"
    )


@pytest.fixture(scope='session')
def setup_opts(request) :
    browser = request.config.getoption("--browser")
    site = request.config.getoption("--site")
    setup = [str(browser), str(site)]
    yield setup
Copy the code
  1. We add two parameter variables “–browser” and “–site” via ConfTest and get the values using this methodrequest.config.getoption("--browser")
  2. The method setup_opts is then passed into the test casetest_bo_check(self, setup_opts):Finally achieve the purpose of value transmission.

Fixtures (scope=’session’) @pytest.fixture(scope=’session’) can be session, module, class, fuction, and so on. The normal name of the case should be test_XXX or xxx_test, otherwise it will not be found.

Encountered pits, and another way to implement

  1. Can’t pass value to setup_class(self)
def setup_class(self) :
    pass
Copy the code
  1. Because you can only pass values to specific cases, it can be a bit annoying when initializing the test environment.
class TestBoCheck:
Here we need to initialize some test environment, but the value can only be passed in the test case, which is a bit awkward
    webflow = FlowsAPI() 
    resourcemaps = ResourceMaps()
    utils = Utils()

    def setup_class(self) :
        pass

    def teardown_class(self) :
        pass

# Pass value here
    def test_bo_check(self, setup_opts) :
        self.utils.set_setup_opts(setup_opts)
     
Copy the code

Another way to do it

Py –pram=setting — fire. Py –pram=setting — fire. Py –pram=setting

python fire.py --env=dev1 --bouser=username --bopsd=123456