1.install
pip install pytest pytest-pep8 pytest-cov
2.write test code
test_hoge.py
import pytest
def TestClass:
def pytest_funcarg__hoge(request):
return Hoge(1)
def test_type(self, hoge):
assert isinstance(hoge, Hoge)
pytest_funcarg__hoge is like setup, which is executed at each test execution and passed as a test argument. It's convenient because you don't need self.hoge. However, this is an old style, and using a decorator is a new way.
test_hoge.py
import pytest
def TestClass:
@pytest.fixture()
def hoge(request):
return Hoge(1)
def test_type(self, hoge):
assert isinstance(hoge, Hoge)
3.run tests
py.test --verbose --cov . --cov-report=html --pep8
4.write production code
hoge.py
class Hoge:
def __init__(self, v):
self.val = v
5.write test code
test_hoge.py
class MockClass:
def method1(self, p1, p2):
pass
class TestClass:
@pytest.fixture()
def mockclass(request):
return MockClass()
def test_method1(self, mockclass, monkeypatch):
monkeypatch.setattr(module1, 'method1', mockclass.method1)
val = module1.method1(p1, p2)
assert val == "..."
Recommended Posts