laserbrain laserbrain - 1 year ago 186
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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download