Parametrized fixture fails when using unittest
Hello, I don't understand why the following test fixture works for the test function, but fails for the test method. Am I doing something wrong, or is this a bug? $ cat t0_mine.py import pytest import unittest @pytest.yield_fixture(params=(0,1,2)) def param1(request): if request.param == 1: pytest.skip('not so good') yield request.param @pytest.fixture(params=('a','b')) def param2(request, param1): return (param1, request.param) @pytest.fixture() def param1a(request, param1): if param1 == 1: return 0 else: return 7 def test_function(param2, param1a): if param2[0] == 1: assert param1a == 0 else: assert param1a == 7 @pytest.mark.usefixtures('param2', 'param1a') $ py.test-3 t0_mine.py -v ==================================== test session starts ==================================== platform linux -- Python 3.3.3 -- pytest-2.5.1 -- /usr/bin/python3 collected 7 items t0_mine.py:25: test_function[a-0] PASSED t0_mine.py:25: test_function[a-1] SKIPPED t0_mine.py:25: test_function[a-2] PASSED t0_mine.py:25: test_function[b-0] PASSED t0_mine.py:25: test_function[b-1] SKIPPED t0_mine.py:25: test_function[b-2] PASSED t0_mine.py:33: TestClass.test_method ERROR ========================================== ERRORS =========================================== __________________________ ERROR at setup of TestClass.test_method __________________________ request = <SubRequest 'param1' for <TestCaseFunction 'test_method'>> @pytest.yield_fixture(params=(0,1,2)) def param1(request):
if request.param == 1:
E AttributeError: 'SubRequest' object has no attribute 'param' t0_mine.py:9: AttributeError ======================= 4 passed, 2 skipped, 1 error in 0.02 seconds ======================== Best, Nikolaus -- Encrypted emails preferred. PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C »Time flies like an arrow, fruit flies like a Banana.«
Hi! You didn't include the test_method, maybe you just forgot the 'self' as first parameter? Regards, Florian Schulze On 10 Feb 2014, at 4:51, Nikolaus Rath wrote:
Hello,
I don't understand why the following test fixture works for the test function, but fails for the test method. Am I doing something wrong, or is this a bug?
$ cat t0_mine.py import pytest import unittest
@pytest.yield_fixture(params=(0,1,2)) def param1(request): if request.param == 1: pytest.skip('not so good')
yield request.param
@pytest.fixture(params=('a','b')) def param2(request, param1): return (param1, request.param)
@pytest.fixture() def param1a(request, param1): if param1 == 1: return 0 else: return 7
def test_function(param2, param1a): if param2[0] == 1: assert param1a == 0 else: assert param1a == 7
@pytest.mark.usefixtures('param2', 'param1a')
$ py.test-3 t0_mine.py -v ==================================== test session starts ==================================== platform linux -- Python 3.3.3 -- pytest-2.5.1 -- /usr/bin/python3 collected 7 items
t0_mine.py:25: test_function[a-0] PASSED t0_mine.py:25: test_function[a-1] SKIPPED t0_mine.py:25: test_function[a-2] PASSED t0_mine.py:25: test_function[b-0] PASSED t0_mine.py:25: test_function[b-1] SKIPPED t0_mine.py:25: test_function[b-2] PASSED t0_mine.py:33: TestClass.test_method ERROR
========================================== ERRORS =========================================== __________________________ ERROR at setup of TestClass.test_method __________________________
request = <SubRequest 'param1' for <TestCaseFunction 'test_method'>>
@pytest.yield_fixture(params=(0,1,2)) def param1(request):
if request.param == 1: E AttributeError: 'SubRequest' object has no attribute 'param'
t0_mine.py:9: AttributeError ======================= 4 passed, 2 skipped, 1 error in 0.02 seconds ========================
Best, Nikolaus
-- Encrypted emails preferred. PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C
»Time flies like an arrow, fruit flies like a Banana.« _______________________________________________ Pytest-dev mailing list Pytest-dev@python.org https://mail.python.org/mailman/listinfo/pytest-dev
Hi, Seems you can't even trust copy and paste anymore... no idea how that happened. Here's the missing part (so no, self is not missing): [...] @pytest.mark.usefixtures('param2', 'param1a') class TestClass(unittest.TestCase): def test_method(self): if self.param2[0] == 1: assert self.param1a == 0 else: assert self.param1a == 7 Thanks, -Nikolaus "Florian Schulze" <florian.schulze-hi6Y0CQ0nG0@public.gmane.org> writes:
Hi!
You didn't include the test_method, maybe you just forgot the 'self' as first parameter?
Regards, Florian Schulze
On 10 Feb 2014, at 4:51, Nikolaus Rath wrote:
Hello,
I don't understand why the following test fixture works for the test function, but fails for the test method. Am I doing something wrong, or is this a bug?
$ cat t0_mine.py import pytest import unittest
@pytest.yield_fixture(params=(0,1,2)) def param1(request): if request.param == 1: pytest.skip('not so good')
yield request.param
@pytest.fixture(params=('a','b')) def param2(request, param1): return (param1, request.param)
@pytest.fixture() def param1a(request, param1): if param1 == 1: return 0 else: return 7
def test_function(param2, param1a): if param2[0] == 1: assert param1a == 0 else: assert param1a == 7
@pytest.mark.usefixtures('param2', 'param1a')
$ py.test-3 t0_mine.py -v ==================================== test session starts ==================================== platform linux -- Python 3.3.3 -- pytest-2.5.1 -- /usr/bin/python3 collected 7 items
t0_mine.py:25: test_function[a-0] PASSED t0_mine.py:25: test_function[a-1] SKIPPED t0_mine.py:25: test_function[a-2] PASSED t0_mine.py:25: test_function[b-0] PASSED t0_mine.py:25: test_function[b-1] SKIPPED t0_mine.py:25: test_function[b-2] PASSED t0_mine.py:33: TestClass.test_method ERROR
========================================== ERRORS =========================================== __________________________ ERROR at setup of TestClass.test_method __________________________
request = <SubRequest 'param1' for <TestCaseFunction 'test_method'>>
@pytest.yield_fixture(params=(0,1,2)) def param1(request):
if request.param == 1: E AttributeError: 'SubRequest' object has no attribute 'param'
t0_mine.py:9: AttributeError ======================= 4 passed, 2 skipped, 1 error in 0.02 seconds ========================
Best, Nikolaus
-- Encrypted emails preferred. PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C
»Time flies like an arrow, fruit flies like a Banana.« _______________________________________________ Pytest-dev mailing list Pytest-dev@python.org https://mail.python.org/mailman/listinfo/pytest-dev
Pytest-dev mailing list Pytest-dev@python.org https://mail.python.org/mailman/listinfo/pytest-dev
-- Encrypted emails preferred. PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C »Time flies like an arrow, fruit flies like a Banana.«
Hi Nikolaus, I'm not sure about your TestClass at all and wasn't aware that using a unitest.TestCase class with parameterisation could even possibly work like this, but it seems to at least partially. However re-writing the TestClass without using unittest it works fine and simple: class TestCase: def test_method(self, param2, param1a): if param2[0] == 1: assert param1a == 0 else: assert param1a == 7 So if you can maybe avoid using the parameterisation mark with unitest.TestCase classes? Regards, Floris On 10 February 2014 16:45, Nikolaus Rath <Nikolaus@rath.org> wrote:
Hi,
Seems you can't even trust copy and paste anymore... no idea how that happened. Here's the missing part (so no, self is not missing):
[...] @pytest.mark.usefixtures('param2', 'param1a') class TestClass(unittest.TestCase): def test_method(self): if self.param2[0] == 1: assert self.param1a == 0 else: assert self.param1a == 7
Thanks, -Nikolaus
"Florian Schulze" <florian.schulze-hi6Y0CQ0nG0@public.gmane.org> writes:
Hi!
You didn't include the test_method, maybe you just forgot the 'self' as first parameter?
Regards, Florian Schulze
On 10 Feb 2014, at 4:51, Nikolaus Rath wrote:
Hello,
I don't understand why the following test fixture works for the test function, but fails for the test method. Am I doing something wrong, or is this a bug?
$ cat t0_mine.py import pytest import unittest
@pytest.yield_fixture(params=(0,1,2)) def param1(request): if request.param == 1: pytest.skip('not so good')
yield request.param
@pytest.fixture(params=('a','b')) def param2(request, param1): return (param1, request.param)
@pytest.fixture() def param1a(request, param1): if param1 == 1: return 0 else: return 7
def test_function(param2, param1a): if param2[0] == 1: assert param1a == 0 else: assert param1a == 7
@pytest.mark.usefixtures('param2', 'param1a')
$ py.test-3 t0_mine.py -v ==================================== test session starts ==================================== platform linux -- Python 3.3.3 -- pytest-2.5.1 -- /usr/bin/python3 collected 7 items
t0_mine.py:25: test_function[a-0] PASSED t0_mine.py:25: test_function[a-1] SKIPPED t0_mine.py:25: test_function[a-2] PASSED t0_mine.py:25: test_function[b-0] PASSED t0_mine.py:25: test_function[b-1] SKIPPED t0_mine.py:25: test_function[b-2] PASSED t0_mine.py:33: TestClass.test_method ERROR
========================================== ERRORS =========================================== __________________________ ERROR at setup of TestClass.test_method __________________________
request = <SubRequest 'param1' for <TestCaseFunction 'test_method'>>
@pytest.yield_fixture(params=(0,1,2)) def param1(request):
if request.param == 1: E AttributeError: 'SubRequest' object has no attribute 'param'
t0_mine.py:9: AttributeError ======================= 4 passed, 2 skipped, 1 error in 0.02 seconds ========================
Best, Nikolaus
-- Encrypted emails preferred. PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C
»Time flies like an arrow, fruit flies like a Banana.« _______________________________________________ Pytest-dev mailing list Pytest-dev@python.org https://mail.python.org/mailman/listinfo/pytest-dev
Pytest-dev mailing list Pytest-dev@python.org https://mail.python.org/mailman/listinfo/pytest-dev
-- Encrypted emails preferred. PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C
»Time flies like an arrow, fruit flies like a Banana.« _______________________________________________ Pytest-dev mailing list Pytest-dev@python.org https://mail.python.org/mailman/listinfo/pytest-dev
-- Debian GNU/Linux -- The Power of Freedom www.debian.org | www.gnu.org | www.kernel.org
participants (3)
-
Florian Schulze -
Floris Bruynooghe -
Nikolaus Rath