How do I get PyCharm to show entire error diffs from pytest?

If you look closely into PyCharm sources, from the whole pytest output, PyCharm uses a single line the to parse the data for displaying in the Click to see difference dialog. This is the AssertionError: <message> line: def test_spam(): > assert v1 == v2 E AssertionError: assert {‘foo’: ‘bar’} == {‘foo’: ‘baz’} E Differing items: … Read more

PyCharm noinspection for whole file?

Is it possible to disable an inspection for the whole file in PyCharm? Yes. NOTE: This answer is for this quoted question only (and not about “Maybe there’s another way to fix these problems? Maybe I’m using py.test incorrectly?”). Settings/Preferences | Appearance & Behavior | Scopes Create a new scope that would include such “unwanted” … Read more

using mocker to patch with pytest

patch requires a path to the function being patched. You could do something like this: import pytest def sum(a, b): return a + b def test_sum1(mocker): mocker.patch(__name__ + “.sum”, return_value=9) assert sum(2, 3) == 9 def test_sum2(mocker): def crazy_sum(a, b): return b + b mocker.patch(__name__ + “.sum”, side_effect=crazy_sum) assert sum(2, 3) == 6 Result: $ … Read more

Pytest use same fixture twice in one function

An alternative is just to copy the fixture function. This is both simple and correctly handles parameterized fixtures, calling the test function with all combinations of parameters for both fixtures. This example code below raises 9 assertions: import pytest @pytest.fixture(params=[0, 1, 2]) def first(request): return request.param second = first def test_double_fixture(first, second): assert False, ‘{} … Read more

Override a pytest parameterized functions name

You’re looking for the ids argument of pytest.mark.parametrize: list of string ids, or a callable. If strings, each is corresponding to the argvalues so that they are part of the test id. If callable, it should take one argument (a single argvalue) and return a string or return None. Your code would look like @pytest.mark.parametrize( … Read more

py.test patch on fixture

I’d say patching via decorator is not the optimal approach here. I’d use the context manager: import pytest from unittest.mock import patch @pytest.fixture(autouse=True) def no_delay(): with patch(‘ConstantsModule.ConstantsClass.DELAY_TIME’, 10): yield This way, patching is cleanly reverted on test teardown.

py.test does not find tests under a class

By convention it searches for Test prefixed test classes (without an init method) eg. # content of test_class.py class TestClass: def test_one(self): x = “this” assert ‘h’ in x def test_two(self): x = “hello” assert hasattr(x, ‘check’) # this works too @staticmethod def test_three(): pass # this doesn’t work #@classmethod #def test_three(cls): # pass See … Read more

indirect=True vs indirect=False in @pytest.mark.parametrize()?

With indirect=True you can parametrize your fixture, False – default value. Example: import pytest @pytest.fixture def fixture_name(request): return request.param @pytest.mark.parametrize(‘fixture_name’, [‘foo’, ‘bar’], indirect=True) def test_indirect(fixture_name): assert fixture_name == ‘baz’ So this example generates two tests. First one gets from fixture_name value foo, because this fixture for this test runs with parametization. Second test gets bar … Read more

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