我正在考虑从我们当前的测试框架鼻子w / nose-testconfig切换到py.test。 任何建议如何使用pytest fixtures覆盖下面专门设置/拆除的代码
class BaseTestCase(unittest.TestCase, Navigation): @classmethod def setUpClass(cls): browser = Browser.getDriver((config['browser']).lower()) cls.driver = EventFiringWebDriver(browser, MyListener()) cls.driver.implicitly_wait(10) cls.driver.maximize_window() try: cls.driver.get(config['url']) except KeyError: cls.driver.get(DEV_ENV_URL) def run(self, result=None): super(BaseTestCase, self).run(MyTestResult(result, self)) @classmethod def tearDownClass(cls): cls.driver.quit()我希望能够传递命令行参数,即url,浏览器,调试等。
I'm thinking about switching from our current testing framework nose w/ nose-testconfig to py.test. Any suggestion how to overwrite the following code specifically setup/teardown below using pytest fixtures
class BaseTestCase(unittest.TestCase, Navigation): @classmethod def setUpClass(cls): browser = Browser.getDriver((config['browser']).lower()) cls.driver = EventFiringWebDriver(browser, MyListener()) cls.driver.implicitly_wait(10) cls.driver.maximize_window() try: cls.driver.get(config['url']) except KeyError: cls.driver.get(DEV_ENV_URL) def run(self, result=None): super(BaseTestCase, self).run(MyTestResult(result, self)) @classmethod def tearDownClass(cls): cls.driver.quit()I'd like to be able to pass command line arguments i.e. url, browser, debug etc.
最满意答案
首先,查看py.test文档。
其次,假设您想要使用fixture而不是setUp/tearDown您需要做的事情很少:创建一个新的文件conftest.py ,您conftest.py在其中测试案例。 如果你把它放在别的地方py.test将找不到它。
def pytest_addoption(parser): parser.addoption("--browser", action="store", default="chrome", help="Type in browser type") parser.addoption("--url", action="store", default=DEV_ENV_URL, help="url") @pytest.fixture(scope='class', autouse=True) def driver(request): browser_name = request.config.getoption("--browser") url = request.config.getoption("--url") driver = Browser(browser_name).getDriver() # driver.get(url) yield driver # Write your setUp before 'yield' driver.quit() # Write tearDown after 'yield'这将使您的所有测试都使用此夹具。 但是还有另一个问题我可以看到你的测试继承自这个BaseTestCase所以如果你没有遵循py.test支持它的命名约定,它将找不到你的测试(阅读文档)。
尽管如此,还有更多的东西。 正如我在文档中所说的一切。
First of all, check out py.test documentation.
Second, there are few things you have to do assuming you want to use fixtures and not setUp/tearDown: Create a new file conftest.py where you test cases are. If you place it somewhere else py.test will not find it.
def pytest_addoption(parser): parser.addoption("--browser", action="store", default="chrome", help="Type in browser type") parser.addoption("--url", action="store", default=DEV_ENV_URL, help="url") @pytest.fixture(scope='class', autouse=True) def driver(request): browser_name = request.config.getoption("--browser") url = request.config.getoption("--url") driver = Browser(browser_name).getDriver() # driver.get(url) yield driver # Write your setUp before 'yield' driver.quit() # Write tearDown after 'yield'This will make all your tests use this fixture. But there is another problem I can see that your tests inherit from this BaseTestCase so if you didn't follow the naming convention that py.test supports it will not find your tests (read the docs on that).
There is a lot more to pytest though. As I've said everything in the docs.
更多推荐
发布评论