【发布时间】:2019-09-11 07:33:58
【问题描述】:
命令行
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)
【问题讨论】:
-
如果没有别的,我真的很好奇你将
unittest.TestCase和 pytest 的固定装置混合在一起的动机。 -
您尚未发布测试函数,但看起来您正在像
setup()那样调用夹具,而不是像setup那样使用它。或者,如果setup夹具没有返回任何内容,那么您可以避免在测试函数中使用它(它已经设置为自动使用)。 -
@tmt 我使用基于 unittest 的测试套件来使用 pytest 作为测试运行器;在路上,我也喜欢 pytest 固定装置,我希望使用它们。是否有可能解决我面临的问题
-
我认为您的问题是 pytest 期望成为夹具及其调用(基于范围、自动使用等)的唯一管理器,而
unittest.TestCase调用setUp方法 automatically 为出色地。所以我不认为setUp应该是一个固定装置。 -
@tmt,yeniv 都是对的;这是因为本机 unittest.TestCase setUp()。我通过在单独的方法中调用它来修复它
标签: python python-3.x pytest