夹具“ setUp”直接调用。灯具不能直接调用

时间:2019-09-11 07:33:58

标签: python python-3.x pytest

命令行

  

python3 -m pytest src / spec / --app = android

conftest.py

import pytest

def pytest_addoption(parser):
    parser.addoption('--app')

@pytest.fixture(scope="session")
def app(request):
    return request.config.getoption("--app")

driver.py

import pytest

class Driver(unittest.TestCase):

    def __init__(self, driver):
        unittest.TestCase.__init__(self, driver)

    @pytest.fixture(autouse=True)
    def setUp(self, app):
        self.app = app

        if self.app == 'ios':
            desired_caps = {}
            desired_caps['platformName'] = 'ios'
            desired_caps['platformVersion'] = ''
            desired_caps['deviceName'] = 'PF'

        elif self.app == 'android':
            desired_caps = {}
            desired_caps['platformName'] = 'Android'
            desired_caps['platformVersion'] = ''
            desired_caps['deviceName'] = 'PF'
            desired_caps['appPackage'] = 'com.wdiodemoapp'
            desired_caps['appActivity'] = 'com.wdiodemoapp.MainActivity'

        self.driver = webdriver.Remote("http://0.0.0.0:4723/wd/hub", desired_caps)

enter image description here

1 个答案:

答案 0 :(得分:1)

问题在于unittestpytest混合在一起的原因。下面修复了它。

def setUp(self):
   Driver.fix

   if self.app == 'ios':
      desired_caps = {}
      desired_caps['platformName'] = 'ios'
      desired_caps['platformVersion'] = ''
      desired_caps['deviceName'] = 'PF'

   elif self.app == 'android':
      desired_caps = {}
      desired_caps['platformName'] = 'Android'
      desired_caps['platformVersion'] = ''
      desired_caps['deviceName'] = 'PF'
      desired_caps['appPackage'] = 'com.wdiodemoapp'
      desired_caps['appActivity'] = 'com.wdiodemoapp.MainActivity'

@pytest.fixture(autouse=True)
def fix(self, app):
   self.app = app