1

I have a simple test below that I run with pytest. It works via the built-in function exec:

def test_exec() -> None:
    foo = ""  # Causes test to fail if not updated by exec
    exec("foo = 5")
    assert isinstance(foo, int)

When run outside of pytest (via ipython), the code works fine. When run via pytest, the exec statement doesn't seem to be run properly.

How can I run an exec function as part of a pytest?


Versions

Python==3.8.5
pytest==6.1.1
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
  • Why are you using `exec`? Do you understand that `exec` cannot modify local variables? Just don't use `exec`, there is *almost certainly* a better way. This question really has nothing to do with `pytest`. – juanpa.arrivillaga Oct 26 '20 at 19:12
  • Needless to say, I had some fundamental misunderstandings on `exec`. The duplicate question pointed me to figuring out the problem. @juanpa.arrivillaga the code in my question was just a minimal repro. You're right, it turns out not to have anything to do with `pytest` – Intrastellar Explorer Oct 26 '20 at 19:39

0 Answers0