Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest is identifying a function as a test case that shouldn't be #342

Closed
pytestbot opened this issue Aug 16, 2013 · 2 comments
Closed

pytest is identifying a function as a test case that shouldn't be #342

pytestbot opened this issue Aug 16, 2013 · 2 comments
Labels
type: bug problem that needs to be addressed

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Emily Bache (BitBucket: emilybache, GitHub: emilybache)


I've written some code to dynamically generate some test cases on a unittest.TestCase. Pytest correctly executes these test cases, but wrongly identifies some of the setup code as a test case, and can't execute it. I'd like pytest to ignore this setup code.

$ python3 -m pytest
=================================== test session starts ====================================
platform darwin -- Python 3.3.2 -- pytest-2.3.5
collected 9 items

test_tennis.py .
test_unittest_tennis.py .......E

========================================== ERRORS ==========================================
_______________________________ ERROR at setup of test_case ________________________________
file ./test_unittest_tennis.py, line 36
def foo(self):
fixture 'self' not found
available fixtures: capfd, monkeypatch, pytestconfig, recwarn, tmpdir, capsys
use 'py.test --fixtures [testpath]' for help on them.

./test_unittest_tennis.py:36
============================ 8 passed, 1 error in 0.08 seconds =============================

The "foo" method on line 36 is not a test case and pytest should not try to execute it. The source code is attached.


@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


you need to add del test_name, test_case as final lines. pytest otherwise sees test_case as a test function at module scope. You could see withpy.test -v or py.test --collectonly what tests actually were generated.

I'd recommend to look into using parametrization (many examples in the fixture docs), however. If your goal is to have test code that executes on unittest, nose and py.test then of course that's doable but you are missing out on a lot of nice pytest features :)

(sidenote: i am gone for about 2 weeks of holiday, see you! :)

@pytestbot
Copy link
Contributor Author

Original comment by Emily Bache (BitBucket: emilybache, GitHub: emilybache):


Brilliant, that explains it. I hadn't realized the test_case would be exposed at module scope. I just renamed it to solve the problem.

I will be doing the same example with pytest, I'll contact you about that separately. Enjoy your holiday!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

1 participant