[PYTHON] pytest summary

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

pytest summary
pytest usage summary
pytest
Python Summary
samba summary
Django Summary
python-pptx summary
pytest memo
Linux Summary
Python summary
Django Summary
pyenv summary
String summary 1
matplotlib summary
AtCoderBeginnerContest180 Review & Summary
Kaggle Summary: Outbrain # 2
LINQ library summary
Sphinx setup summary
AtCoderBeginnerContest181 Review & Summary
Digital technology summary
AtCoderBeginnerContest182 Review & Summary
Python tutorial summary
blockdiag grammar summary
AtCoderBeginnerContest183 Review & Summary
Linux Command Summary
AtCoderBeginnerContest179 Review & Summary
update, upgrade summary
Standard input summary
Django filter summary
python related summary
2020 IT Conference Summary
Visualize summary statistics
Docker command summary
Python basics summary
Kaggle related summary