What is the canonical way to isolate unit tests in Python with assertion-style tests? -


i asked question , realized i'd know if there's way achieve 'expectation' isolation assertion-style tests. i've copy , pasted simple example of mean 'expectation' isolation.

i'm relatively new python, coming ruby/javascript , having testing libraries rspec , jasmine has provided me ability isolate 'expectations' when testing single function. since there doesnt seem 'expectation'-style library python actively maintained, wondering if there's way achieve equivalent testing style either unittest or pytest (the 2 popular python testing libraries 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()          'returns 1 more arguments value':             expect f.bar(3) == 4          'raises error if no argument passed in':             expect f.bar() raiseerror 

unittest/assertion-style

test_foo.py

 class foo():      def test_bar(x):          x = 3          self.assertequal(4)          x = none          self.assertraises(error) 

the unittest module part of standard library.

class testfoo(unittest.testcase):     def setup(self):         self.f = foo()      def test_bar(self):         # assertequals deprecated, use assertequal instead         self.assertequals(self.f.bar(3), 4)      def test_missing_argument(self):         self.assertraises(typeerror, self.f.bar) 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -