Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running with pytest parametrize fixture I am getting `self` is not defined

Tags:

python

pytest

I have this class:

accept.py

class AcceptC(object):

    def __init__(self):
        self.minimum = 30
        self.maximum = 40

and the unittest:

accept_test.py

import unittest
import pytest

from app.accept import AcceptC


class TestAcceptC(unittest.TestCase):

    def setUp(self):
        self.accept = AcceptC()


@pytest.mark.parametrize(
    "minimum, maximum, expected_min, expected_max", [
        ("13", "5", 30, 40),
        ("30", "40", 30, 40),
    ])
def test_init_returns_correct_results(minimum, maximum, expected_min, expected_max):
    expected_min = self.accept.minimum
    expected_max = self.accept.maximum
    self.assertEqual(minimum, expected_min)
    self.assertEqual(maximum, expected_max)


if __name__ == "__main__":
    unittest.main()

When running with pytest, I am getting an error:

NameError: name 'self' is not defined

I saw also that I cannot use self in the test function as argument.

Lastly, is there a way to avoid using:

expected_min = self.accept.minimum
expected_max = self.accept.maximum

and use immediately self.accept.minimum?

If I use self as an argument to the test function, it returns:

fixture 'self' not found

like image 451
George Avatar asked Jan 26 '17 08:01

George


People also ask

What is the syntax to run a parameterized test in pytest?

you can put @pytest. mark. parametrize style parametrization on the test functions to parametrize input/output values as well.

What is pytest Mark Parametrize do?

The @pytest. mark. parametrize() decorator lets you parameterize arguments of the testing function independent of fixtures you created.

Can you parameterize a pytest fixture?

pytest enables test parametrization at several levels: pytest. fixture() allows one to parametrize fixture functions.


1 Answers

The problem is simply because of:

class TestAcceptC(unittest.TestCase):

This inheritance of unittest.TestCase will break 'self' in regards to pytest fixtures.

information: https://docs.pytest.org/en/3.0.1/unittest.html

like image 187
symonk Avatar answered Nov 09 '22 22:11

symonk