You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a function in a module that depends on a global variable, and I'd like to test this function using pytest. But when the function gets to the global variable, it says it's not defined. Note: the function works fine when I test it manually. Here's a minimal example that illustrates the problem.
In the file scratch.py I have:
def bar():
global flag
if flag:
return 1
else:
return 0
Then, in scratch_test.py I have:
import pytest
import scratch
def test_foo():
global flag
flag = True
assert scratch.bar() == 1
Now, from the command line:
$ pytest scratch_test.py
=============================================================== test session starts ===============================================================
platform darwin -- Python 3.6.0, pytest-3.1.1, py-1.4.34, pluggy-0.4.0
rootdir: /Users/ksb/computation/science/kvasir, inifile:
collected 1 items
scratch_test.py F
==================================================================== FAILURES =====================================================================
____________________________________________________________________ test_foo _____________________________________________________________________
def test_foo():
global flag
flag = True
> assert scratch.bar() == 1
scratch_test.py:7:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
def bar():
global flag
> if flag:
E NameError: name 'flag' is not defined
scratch.py:3: NameError
============================================================ 1 failed in 0.03 seconds =============================================================
KEVINs-MacBook-Pro-2:kvasir ksb$
But, if I do $ python scratch_test.py I get no error, it runs fine.
So is the global variable not being passed during testing for some reason? I searched the pytest docs, and SO but didn't find anything that seems relevant.
The text was updated successfully, but these errors were encountered:
@kescoboscratch.flag = True should work for you - its commonly suggested not to use globals in such a way as it will add maintenance cost and trouble somewhere down the tile
@RonnyPfannschmidt Thanks! That's actually the solution I think I needed. After some digging, I arrived at the fact that what I was doing is not recommended - the confusion comes from the fact that it was working when I did the same design pattern in the script, or so it seems.
In any case, I'll let the mystery hang and just do some reworking.
Just posted over at SO, but thought I would do so here as well.
I have a function in a module that depends on a global variable, and I'd like to test this function using pytest. But when the function gets to the global variable, it says it's not defined. Note: the function works fine when I test it manually. Here's a minimal example that illustrates the problem.
In the file scratch.py I have:
Then, in scratch_test.py I have:
Now, from the command line:
But, if I do $ python scratch_test.py I get no error, it runs fine.
So is the global variable not being passed during testing for some reason? I searched the pytest docs, and SO but didn't find anything that seems relevant.
The text was updated successfully, but these errors were encountered: