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
you can put @pytest. mark. parametrize style parametrization on the test functions to parametrize input/output values as well.
The @pytest. mark. parametrize() decorator lets you parameterize arguments of the testing function independent of fixtures you created.
pytest enables test parametrization at several levels: pytest. fixture() allows one to parametrize fixture functions.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With