KeyError in module ‘threading’ after a successful py.test run

I observed a similar issue and decided to see what’s going on exactly – let me describe my findings. I hope someone will find it useful. Short story It is indeed related to monkey-patching the threading module. In fact, I can easily trigger the exception by importing the threading module before monkey-patching threads. The following … Read more

Pytest where to store expected data

I had a similar problem once, where I have to test configuration file against an expected file. That’s how I fixed it: Create a folder with the same name of your test module and at the same location. Put all your expected files inside that folder. test_foo/ expected_config_1.ini expected_config_2.ini test_foo.py Create a fixture responsible for … Read more

Using pytest with a src layer

Recommended approach for pytest>=7: use the pythonpath setting Recently, pytest has added a new core plugin that supports sys.path modifications via the pythonpath configuration value. The solution is thus much simpler now and doesn’t require any workarounds anymore: pyproject.toml example: [tool.pytest.ini_options] pythonpath = [ “src” ] pytest.ini example: [pytest] pythonpath = src The path entries … Read more

How can I repeat each test multiple times in a py.test run?

One possible strategy is parameterizing the test in question, but not explicitly using the parameter. For example: @pytest.mark.parametrize(‘execution_number’, range(5)) def run_multiple_times(execution_number): assert True The above test should run five times. Check out the parametrization documentation: https://pytest.org/latest/parametrize.html

Test discovery failure when tests in different directories are called the same

Putting an __init__.py is one way of resolving the conflict. Unlike nose, current pytest does not try to unload test modules in order to import test modules with the same import name. I used to think it’s a bit magic to do this auto-unimporting and might mess up people’s expectation from what the import mechanism … Read more

TypeError: attrib() got an unexpected keyword argument ‘convert’

pytest seems to have the package attrs as a dependency. attrs==19.2.0 was released around 2019-10-01 17:00 UTC. This seems to cause the problem above. Switching back to attrs==19.1.0 fixes the problem. Just do the following: pip install attrs==19.1.0 NOTE: I expect that the issue will be resolved either by attrs or pytest soon by releasing … Read more

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