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.
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.