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)
source
share