Logging within pytest tests

Since version 3.3, pytest supports live logging, meaning that all the log records emitted in tests will be printed to the terminal immediately. The feature is documented under Live Logs section. Live logging is disabled by default; to enable it, set log_cli = 1 in the pyproject.toml1 or pytest.ini2 config. Live logging supports emitting to … Read more

How do I correctly setup and teardown for my pytest class with tests?

According to Fixture finalization / executing teardown code, the current best practice for setup and teardown is to use yield instead of return: import pytest @pytest.fixture() def resource(): print(“setup”) yield “resource” print(“teardown”) class TestResource: def test_that_depends_on_resource(self, resource): print(“testing {}”.format(resource)) Running it results in $ py.test –capture=no pytest_yield.py === test session starts === platform darwin — … Read more

Pass a parameter to a fixture function

This is actually supported natively in py.test via indirect parametrization. In your case, you would have: @pytest.fixture def tester(request): “””Create tester object””” return MyTester(request.param) class TestIt: @pytest.mark.parametrize(‘tester’, [[‘var1’, ‘var2’]], indirect=True) def test_tc1(self, tester): tester.dothis() assert 1

pytest: assert almost equal

I noticed that this question specifically asked about py.test. py.test 3.0 includes an approx() function (well, really class) that is very useful for this purpose. import pytest assert 2.2 == pytest.approx(2.3) # fails, default is ± 2.3e-06 assert 2.2 == pytest.approx(2.3, 0.1) # passes # also works the other way, in case you were worried: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)