laserbrain laserbrain - 1 month ago 6
Python Question

Testing class methods with pytest

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
we like to test:

class SuperCool(object):

def action(self, x):
return x * x

How does the according test class in
have to look?

class TestSuperCool():

def test_action(self, x):

How can
be used to test


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