Testing class methods with pytest

Well, one way is to just create your object within the test method and interact with it from there:

def test_action(self, x):
    o = SuperCool()
    assert o.action(2) == 4

You can apparently use something like the classic setup and teardown style unittest using the methods here: http://doc.pytest.org/en/latest/xunit_setup.html

I'm not 100% sure on how they are used because the documentation for pytest is terrible.

Edit: yeah so apparently if you do something like

class TestSuperCool():
    def setup(self):
        self.sc = SuperCool()

    ... 

    # test using self.sc down here

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

Tags:

Python

Pytest