I need to test some code which has border effects so my tests need to have different Python interpreter.
Is there a way to tell pytest to use a new Python instance of the python interpreter for a given Test Case.
For example, I have a class C1 that sets an environment which is needed before instantiating another class C2 (I know, not a proper implementation :) ). I need to test: * Class C1 is instantiated, then C2 is instantiated => OK * Class C2 is instantiated => Raise exception
The following code would not be proper because of the border effect
def test_OK():
C1()
C2()
def test_NOK():
with pytest.raises(Exception):
C2()
I used a workaround by creating external files run with subprocess but it is ugly like hell.
from pathlib import Path
import subprocess
import pytest
test_dir = Path(__file__).parent.absolute()
python_lib = test_dir.parent / "PythonLib"
no_standard = """
from a661test import TestFramework
from a661libgen_rev6_v1 import Label
framework = TestFramework()
framework.create_df([Label(1).define()])
"""
with_standard = """
from a661test import TestFramework
from a661libgen_rev6_v1 import Label, Standard_rev6_v1
Standard_rev6_v1()
framework = TestFramework()
framework.create_df([Label(1).define()])
"""
class Test_Framework_Nominal:
def tc_A661_TESTFRAMEWORK_FRAMEWORK_TC_R_01(self, monkeypatch):
"""Checking an exception is raised when Standard not set"""
monkeypatch.setenv("PYTHONPATH", str(python_lib))
script = Path("script.py")
script.write_text(no_standard)
with pytest.raises(Exception):
subprocess.run(["python", str(script)], check=True)
def tc_A661_TESTFRAMEWORK_FRAMEWORK_TC_N_02(self, monkeypatch):
"""Checking no exception is raised if Standard is set"""
monkeypatch.setenv("PYTHONPATH", str(python_lib))
script = Path("script.py")
script.write_text(with_standard)
subprocess.run(["python", str(script)], check=True)
Is there some magic option, or plugin / setting of conftest.py that would allow something like:
@pytest.mark.new_python_instance
def test_OK():
C1()
C2()
@pytest.mark.new_python_instance
def test_NOK():
with pytest.raises(Exception):
C2()
Thanks
Aucun commentaire:
Enregistrer un commentaire