Skip to content

Passing global variable from test function #2472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kescobo opened this issue Jun 5, 2017 · 3 comments
Closed

Passing global variable from test function #2472

kescobo opened this issue Jun 5, 2017 · 3 comments

Comments

@kescobo
Copy link

kescobo commented Jun 5, 2017

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:

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.

@kescobo
Copy link
Author

kescobo commented Jun 5, 2017

Wait - something else is wrong... sorry. Will come back and edit if I figure that out.

@kescobo kescobo closed this as completed Jun 5, 2017
@RonnyPfannschmidt
Copy link
Member

@kescobo scratch.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

@kescobo
Copy link
Author

kescobo commented Jun 6, 2017

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants