How do I correctly setup and teardown for my pytest class with tests?

I am using selenium for end to end testing and I can’t get how to use setup_class and teardown_class methods. I need to set up browser in setup_class method, then perform a bunch of tests defined as class methods and finally quit browser in teardown_class method. But logically it seems like a bad solution, because … Read more

PATH issue with pytest ‘ImportError: No module named YadaYadaYada’

I used easy_install to install pytest on a mac and started writing tests for a project with a file structure likes so: repo/ |–app.py |–settings.py |–models.py |–tests/ |–test_app.py run py.test while in the repo directory, everything behaves as you would expect but when I try that same thing on either linux or windows (both have … Read more

How to print to console in pytest?

I’m trying to use TDD (test-driven development) with pytest. pytest will not print to the console when I use print. I am using pytest my_tests.py to run it. The documentation seems to say that it should work by default: http://pytest.org/latest/capture.html But: import myapplication as tum class TestBlogger: @classmethod def setup_class(self): self.user = “alice” self.b = … Read more

Is there a way to specify which pytest tests to run from a file?

Is there a way to select pytest tests to run from a file? For example, a file foo.txt containing a list of tests to be executed: tests_directory/foo.py::test_001 tests_directory/bar.py::test_some_other_test Or, is there a way to select multiple tests, having no common pattern in test name, from different directories with pytest? pytest -k <pattern> allows a single … Read more

How to properly assert that an exception gets raised in pytest?

Code: # coding=utf-8 import pytest def whatever(): return 9/0 def test_whatever(): try: whatever() except ZeroDivisionError as exc: pytest.fail(exc, pytrace=True) Output: ================================ test session starts ================================= platform linux2 — Python 2.7.3 — py-1.4.20 — pytest-2.5.2 plugins: django, cov collected 1 items pytest_test.py F ====================================== FAILURES ====================================== ___________________________________ test_whatever ____________________________________ def test_whatever(): try: whatever() except ZeroDivisionError as … Read more