How can I use global variables through tests?

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
nicoddemuscommented, Jan 15, 2018

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™; if you look it up you will find dozens of posts/articles explaining why this is bad in general.

2reactions
RonnyPfannschmidtcommented, Jan 15, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I use a global variable in unit testing? - Stack Overflow
Create a method to run on initialization and setup any object that needs to be available for all tests within that method.
Read more >
Unit Tests: how to, when having global variables - Luís Nóbrega
Imagine you are developing in some legacy application, and have to use global variables. How can you do unit tests, maintaining the use...
Read more >
Declaring global variables for testing (Ada) - IBM
Accessing Global Variables. The extra global variable package is visible from within all units of the test driver. Variables can be accessed like...
Read more >
Bring Legacy Code under tests by handling global variables
Global variables can make tests challenging to write.​​ But tests are the most useful tool you have to feel confident refactoring Legacy Code,...
Read more >
How to use Global Variables between test suites while ...
Hi, you have to first define Global Variable with value or null value ` [local] then in testcase use it CustomKeywords.'com.global.variables ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found