Skip to content

How can I use global variables through tests? #3118

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
UANEMESIS opened this issue Jan 15, 2018 · 10 comments
Closed

How can I use global variables through tests? #3118

UANEMESIS opened this issue Jan 15, 2018 · 10 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@UANEMESIS
Copy link

In my example:

@pytest.fixture
def global_var():
    pytest.global_variable_1 = 100

def test_1(global_var):
	pytest.global_variable_1 +=1
	print(pytest.global_variable_1)

def test_2(global_var):
	pytest.global_variable_1 +=1
	print(pytest.global_variable_1)

def test_3(global_var):
	pytest.global_variable_1 +=1
	print(pytest.global_variable_1)

Global variable pytest.global_variable works only in test_1. output:
101
101
101

What do I wrong in?

@RonnyPfannschmidt
Copy link
Member

for each function you set it back to 100 in the setup, then increment it by 1

so its doing exactly what you told it to

@nicoddemus
Copy link
Member

Every time a fixture is requested in a test (def test_1(global_var)) the fixture function will be called and the result will be passed as the parameter value: what's happening here is that because each test requires the global variable, global_var is being executed each time and resetting the value of pytest.global_variable_1 to 100.

Fixtures are meant to avoid using global variables in the first place, they are meant to provide resources to test functions that are isolated and particular for each test.

You can update your code as follows:

import pytest
pytest.global_variable_1 = 100

def test_1():
	pytest.global_variable_1 +=1
	print(pytest.global_variable_1)

def test_2():
	pytest.global_variable_1 +=1
	print(pytest.global_variable_1)

def test_3():
	pytest.global_variable_1 +=1
	print(pytest.global_variable_1)

This will behave like you expect it to.

Disclaimer: using global variables in general (and in testing in particular) is generally a bad idea(tm); if you look it up you will find dozens of posts/articles explaining why this is bad in general.

@nicoddemus nicoddemus added the type: question general question, might be closed after 2 weeks of inactivity label Jan 15, 2018
@UANEMESIS
Copy link
Author

Thank u!

@FelipeJung
Copy link

@nicoddemus I just ran into this problem and I have a question on the declarations of variables in pytest like this...
I'd like to know if, when you declare a global variable like this:
pytest.globalvariable = 0
Would this variable be global to all session of tests or only for this module?

For example, if I am running several modules in a pytest session, can tests on other modules access this variable? And if they can, is there a way of making this variable global only for the module?

Thanks in advance!

@UANEMESIS
Copy link
Author

@nicoddemus I just ran into this problem and I have a question on the declarations of variables in pytest like this...
I'd like to know if, when you declare a global variable like this:
pytest.globalvariable = 0
Would this variable be global to all session of tests or only for this module?

For example, if I am running several modules in a pytest session, can tests on other modules access this variable? And if they can, is there a way of making this variable global only for the module?

Thanks in advance!

For all session (for all tests in file)

@FelipeJung
Copy link

@nicoddemus I just ran into this problem and I have a question on the declarations of variables in pytest like this...
I'd like to know if, when you declare a global variable like this:
pytest.globalvariable = 0
Would this variable be global to all session of tests or only for this module?
For example, if I am running several modules in a pytest session, can tests on other modules access this variable? And if they can, is there a way of making this variable global only for the module?
Thanks in advance!

For all session (for all tests in file)

Yeah, right... But what if my session is composed by several different files? Would every test in every file have access to that variable? Or only the tests on the file that I declared this variable have access to it?
Thanks!

@UANEMESIS
Copy link
Author

@nicoddemus I just ran into this problem and I have a question on the declarations of variables in pytest like this...
I'd like to know if, when you declare a global variable like this:
pytest.globalvariable = 0
Would this variable be global to all session of tests or only for this module?
For example, if I am running several modules in a pytest session, can tests on other modules access this variable? And if they can, is there a way of making this variable global only for the module?
Thanks in advance!

For all session (for all tests in file)

Yeah, right... But what if my session is composed by several different files? Would every test in every file have access to that variable? Or only the tests on the file that I declared this variable have access to it?
Thanks!

I think it will be applied for all .py file. But, not sure at 100%, just try and you will know^)

@FelipeJung
Copy link

@nicoddemus I just ran into this problem and I have a question on the declarations of variables in pytest like this...
I'd like to know if, when you declare a global variable like this:
pytest.globalvariable = 0
Would this variable be global to all session of tests or only for this module?
For example, if I am running several modules in a pytest session, can tests on other modules access this variable? And if they can, is there a way of making this variable global only for the module?
Thanks in advance!

For all session (for all tests in file)

Yeah, right... But what if my session is composed by several different files? Would every test in every file have access to that variable? Or only the tests on the file that I declared this variable have access to it?
Thanks!

I think it will be applied for all .py file. But, not sure at 100%, just try and you will know^)

Thanks for your help @UANEMESIS ...

I tested this declaration in different files containing tests and it I concluded that when using a declaration like
pytest.any_variable = any_value
this variable will be global for the entire session. You can declare it in any file and use it in any test function you like.

So I guess that this declaration is indeed dangerous to use. That is why people consider using this as a bad practice.

@amitkhomane
Copy link

the result will be passed as the parameter value: what's happening here is that because each test requires the global variable, global_var is being executed each time and resetting the value of pyte

``

Every time a fixture is requested in a test (def test_1(global_var)) the fixture function will be called and the result will be passed as the parameter value: what's happening here is that because each test requires the global variable, global_var is being executed each time and resetting the value of pytest.global_variable_1 to 100.

Fixtures are meant to avoid using global variables in the first place, they are meant to provide resources to test functions that are isolated and particular for each test.

You can update your code as follows:

import pytest
pytest.global_variable_1 = 100

def test_1():
	pytest.global_variable_1 +=1
	print(pytest.global_variable_1)

def test_2():
	pytest.global_variable_1 +=1
	print(pytest.global_variable_1)

def test_3():
	pytest.global_variable_1 +=1
	print(pytest.global_variable_1)

This will behave like you expect it to.

Disclaimer: using global variables in general (and in testing in particular) is generally a bad idea(tm); if you look it up you will find dozens of posts/articles explaining why this is bad in general.

This seems to be deprecated now, isn't it ? @RonnyPfannschmidt @nicoddemus

@nicoddemus
Copy link
Member

I'd like to know if, when you declare a global variable like this:
pytest.globalvariable = 0
Would this variable be global to all session of tests or only for this module?

For the entire session, but there's nothing special about pytest here; it is just a Python module as any other. You will get the same effect if you set the same variable to sys or os for example (or any other module).

This seems to be deprecated now, isn't it ? @RonnyPfannschmidt @nicoddemus

Not sure I understand the question, what do you mean by "this" in this context?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

5 participants