mercredi 11 novembre 2020

Setting globals in pytest class methods

I have an application that reads some config settings from a remote source upon startup and stores them in global scope. A function in this application uses a setting in that global config. Functionally it ends up looking something like this...

def define_globals():
    global MY_THRESHOLD
    MY_THRESHOLD = 123

def my_func(my_input):
    return my_input > MY_THRESHOLD

def run():
    define_globals()
    my_func(122)

I would like to test my_func using pytest but MY_THRESHOLD is not defined during the test. Kinda of new to testing so looked into fixtures a bit. Have something like this but still not finding that global when running the test.

import pytest
import my_file

@pytest.fixture()
def set_gloabls():
    global MY_THRESHOLD
    MY_THRESHOLD = 123

class TestMyApplication(object):

    @pytest.mark.usefixtures("set_gloabls")
    def test_my_func(self):
        assert my_file.my_func(122) == False

I guess I thought that a fixture would be working in the scope of the file being tested? IDK having a hard time groking how to do this without a code change in the application.

Aucun commentaire:

Enregistrer un commentaire