pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Tests in different files will interfere each other

Open tedzhao226 opened this issue 6 years ago • 5 comments

While I doing test of each module, each variable in different file imported from the same module will not interfere each other. But when I do whole test on test folder, two variables are linked to the same object, even they are separated in different modules.

To prevent this situation, I need to do a deepcopy, when I need to update the variable. It is a nature of design, or it is a bug?

A sample structure show below:

project structure

.
├── src
│   └── tools.py
└── test
    ├── test_a.py
    └── test_b.py

Codes:

# file src/tools.py

a = {1 : 1}
# file tests/test_a.py

import pytest
import copy
from src.tools import a

def test_a():
    a.update({2: 2})
   
def test_copy_a():
    a_copy = copy.deepcopy(a)
    a_copy.update({2: 2})
# file tests/test_b.py
import pytest
from src.tools import a

def test_b():
    print(a)

## result: a = {1: 1, 2:2}
## if I run test_copy_a, intead of test_a, var a will not change on file tests/b.py
## result: a = {1: 1}

tedzhao226 avatar Dec 19 '19 13:12 tedzhao226

This isn't anything pytest specific - it's just how Python works.

The-Compiler avatar Dec 19 '19 13:12 The-Compiler

Hi @tade0726,

This is not related to pytest, it is how Python works: test_a updates the global variable a, while test_copy_a creates a copy of a, changes it, then throws it away. That's why executing only test_copy_a has no effect on a at all.

I suggest to play with it a bit on the prompt:

>>> a = {1: 1}
>>> def update_a():
...   a.update({2: 2})
...
>>> def copy_a():
...   x = a.copy()
...   x.update({3: 3})
...
>>> update_a()
>>> a
{1: 1, 2: 2}
>>> copy_a()
>>> a
{1: 1, 2: 2}
>>>

nicoddemus avatar Dec 19 '19 13:12 nicoddemus

@The-Compiler @nicoddemus

Maybe I didn't address it right, the sample codes are not in the same file. Variable A is imported into different modules, modules a.py and b.py, both file under tests folder.

What I done test on each module, it works fine.

But when I run tests on tests folder, the dict a update in a.py will change Var a'in b.py too. In normal situation, var a import to different file should not be modified.

tedzhao226 avatar Dec 19 '19 18:12 tedzhao226

@tade0726 Modules are singletons in Python, i.e. only one module src.tools and one a exists. If you modify it from somewhere, it's going to be the same thing everywhere - again, in Python in general, not just with pytest.

The-Compiler avatar Dec 19 '19 18:12 The-Compiler

@The-Compiler TKS for replying so quickly. I understand that when running tests on test folder whole tests module work as singletons. In my assumption, I wrote each test file as a parallel test case. So it is kind of bugging me for a while, when I run tests together, one single test case will always fail, but after I rerun that test case along, it pass.

tedzhao226 avatar Dec 19 '19 19:12 tedzhao226