What is the canonical way to isolate unit tests in Python using assertion type tests?

I recently asked this question about this and realized that I would also like to know if there is a way to achieve "expectation" of isolation using assertion type tests. I copied and pasted a simple example of what I mean by "waiting" for isolation.

I am relatively new to Python, coming from Ruby / JavaScript, and testing libraries such as Rspec and Jasmine has provided me with the ability to isolate "expectations" when testing a single function. Since there does not seem to be an expected style library for Python that is actively supported, I was wondering if there is a way to achieve an equivalent testing style with unittest or pytest (the 2 most popular Python testing libraries from what I understand).

foo.py

class Foo():
    def bar(self, x):
        return x + 1

Expectation-Style / Describe-It

test_foo.py

describe Foo:
    describe self.bar:
        before_each:
            f = Foo()

        it 'returns 1 more than its arguments value':
            expect f.bar(3) == 4

        it 'raises an error if no argument is passed in':
            expect f.bar() raiseError

UnitTest / Style Approval

test_foo.py

 class Foo():
     def test_bar(x):
         x = 3
         self.assertEqual(4)
         x = None
         self.assertRaises(Error)
+4
source share
1 answer

The module unittestis part of the standard library.

class TestFoo(unittest.TestCase):
    def setUp(self):
        self.f = Foo()

    def test_bar(self):
        # assertEquals is deprecated, use assertEqual instead
        self.assertEquals(self.f.bar(3), 4)

    def test_missing_argument(self):
        self.assertRaises(TypeError, self.f.bar)
+1
source

Source: https://habr.com/ru/post/1542937/