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

cannot decorate @pytest.mark.usefixtures("xy") onto class inherits from unittest.TestCase #3484

Closed
willhyper opened this issue May 17, 2018 · 7 comments
Labels
topic: parametrize related to @pytest.mark.parametrize type: question general question, might be closed after 2 weeks of inactivity

Comments

@willhyper
Copy link

minimum example:

import unittest
import pytest

xy1 = (1, 2)
xy2 = (3, 4)


@pytest.fixture(scope="class", params=[xy1, xy2])
def xy(request):
    request.cls.x, request.cls.y = request.param


@pytest.mark.usefixtures("xy")
class TestClassTestCase(unittest.TestCase): # Failed: The requested fixture has no parameter defined for the current test.
    def test_method1(self):
        print('access fixture attributes', self.x, self.y)


@pytest.mark.usefixtures("xy")
class TestClassObject(object): # ok!
    def test_method1(self):
        print('access fixture attributes', self.x, self.y)

motivation of such example:

pytest allows @pytest.mark.parametrize decorated on test functions on module level. One could have developed tests as

import pytest

xy1 = (1, 2)
xy2 = (3, 4)
xy = [xy1, xy2]

@pytest.mark.parametrize('x,y', xy)
def test_fixture(x, y):
    print('access fixture attributes', x, y)

my incentive on applying fixture onto class level is to leverage predefined test methods in super class, such as assertRaises in unittest.TestClass. if @pytest.mark.usefixtures cannot decorate on general classes, the use of it is less obvious, as @pytest.mark.parametrize functions the same and easier to write.

@pytestbot pytestbot added topic: parametrize related to @pytest.mark.parametrize type: question general question, might be closed after 2 weeks of inactivity labels May 17, 2018
@RonnyPfannschmidt
Copy link
Member

parametrization is not supported for unittest tests

btw, this is a duplicate issue i will find the original one later

@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #541 (Can't use pytest.mark.parametrize inside unittest class), #378 (@pytest.mark.usefixtures take no effects on fixtures), #1464 (pytest.mark on TestCase affects base classes), #3308 (Question: Can someone explain the below uses of @pytest.mark.usefixtures), and #2806 (mark.usefixtures registered fixtures of all test class subclasses invoked despite -k).

@RonnyPfannschmidt
Copy link
Member

oh - gitmate already did - the issue is #541

@willhyper
Copy link
Author

Thanks @RonnyPfannschmidt !
Do you mind elaborating "parametrization is not supported for unittest tests"?
Shall I understand it as my use case is not supported? or, my use case is not valid so no support?

Also I don't think #541 is a duplication. Here is the reason:
#541 decorates @pytest.mark.parametrize on class method(s). Only methods got decorated are affected. #541 anyway is a dead end because the first argument in every class method is "self", which cannot be parameterized.
Here I decorate @pytest.mark.usefixtures on class itself, which propagates fixtures to all methods in the class.

@RonnyPfannschmidt
Copy link
Member

the use case is invalid - parametrized fixtures and tests do not support unittest - and will never support unittest

parameterize and fixtures with params are only availiable to native pytest tests

@RonnyPfannschmidt
Copy link
Member

also #541 applies since both parameterize and fixture(param=...) use the same mechanism

@willhyper
Copy link
Author

Agh, got it. Thanks for clarifying. You the best!

otubo added a commit to otubo/cloud-init that referenced this issue Jul 6, 2020
Splitting test_swap_creation_method into 3 different cases for different
filesystems. Leaving the methods way of asserting the test with only
cc_mounts.handle() function. Like test_swap_integrity() does.

Signed-off-by: Eduardo Otubo <[email protected]>

[0] pytest-dev/pytest#3484
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: parametrize related to @pytest.mark.parametrize type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants