In the documentation of pytest various examples for test cases are listed. Most of them show the test of functions. But I’m missing an example of how to test classes and class methods. Let’s say we have the following class in the module cool.py we like to test:
cool.py
class SuperCool(object): def action(self, x): return x * x
How does the according test class in tests/test_cool.py have to look?
tests/test_cool.py
class TestSuperCool(): def test_action(self, x): pass
How can test_action() be used to test action()?
test_action()
action()
All you need to do to test a class method is instantiate that class, and call the method on that instance:
def test_action(self): sc = SuperCool() assert sc.action(1) == 1
1.4m articles
1.4m replys
5 comments
57.0k users