Python unittest正确设置全局变量

2024-10-04 11:26:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的方法,根据方法参数将全局变量设置为True或False

此全局变量称为feedback,默认值为False

当我调用setFeedback('y')时,全局变量将更改为feedback = True。 当我调用setFeedback('n')时,全局变量将更改为feedback = False

现在,我尝试使用Python中的unittest来测试这一点:

class TestMain(unittest.TestCase):

    def test_setFeedback(self):

        self.assertFalse(feedback)
        setFeedback('y')
        self.assertTrue(feedback)

当我运行这个测试时,我得到以下错误:AssertionError: False is not true

因为我知道该方法工作正常,所以我假设全局变量以某种方式重置。然而,由于我对Python环境还是非常陌生,我不知道自己到底做错了什么

我已经读过一篇关于mock的文章,但是由于我的方法改变了一个全局变量,我不知道mock是否能解决这个问题

如能提出建议,我将不胜感激

代码如下:

main.py:

#IMPORTS
from colorama import init, Fore, Back, Style
from typing import List, Tuple

#GLOBAL VARIABLE
feedback = False

#SET FEEDBACK METHOD
def setFeedback(feedbackInput):
    """This methods sets the feedback variable according to the given parameter.
       Feedback can be either enabled or disabled.

    Arguments:
        feedbackInput {str} -- The feedback input from the user. Values = {'y', 'n'}
    """

    #* ACCESS TO GLOBAL VARIABLES
    global feedback

    #* SET FEEDBACK VALUE
    # Set global variable according to the input
    if(feedbackInput == 'y'):

        feedback = True
        print("\nFeedback:" + Fore.GREEN + " ENABLED\n" + Style.RESET_ALL)
        input("Press any key to continue...")

        # Clear the console
        clearConsole()

    else:
        print("\nFeedback:" + Fore.GREEN + " DISABLED\n" + Style.RESET_ALL)
        input("Press any key to continue...")

        # Clear the console
        clearConsole()

test_main.py:

import unittest
from main import *

class TestMain(unittest.TestCase):

    def test_setFeedback(self):

        self.assertFalse(feedback)
        setFeedback('y')
        self.assertTrue(feedback)


if __name__ == '__main__':
    unittest.main()

Tags: theto方法fromimportselffalsetrue
2条回答

你不需要嘲笑任何东西;在运行每个测试之前,您只需要确保全局变量处于已知状态。另外,使用from main import *在测试模块中创建一个名为feedback新的全局,与setFeedback正在修改的main.feedback不同

import main

class TestMain(unittest.TestCase):

    def setUp(self):
        main.feedback = False

    def test_setFeedback(self):

        self.assertFalse(feedback)
        main.setFeedback('y')
        self.assertTrue(feedback)

你的测试有两个问题

首先,在feedback函数中使用input,这将暂停测试,直到输入键为止。您可能应该模拟input。也可以考虑调用^ {< CD1> }不属于^ {< CD5> }(参见@ CHEPENER注释)。

其次,from main import *在这里不起作用(除了作为bad style),因为通过这种方式,您可以在测试模块中创建全局变量的副本-变量本身的更改不会传播到副本。您应该导入模块,以便访问模块中的变量

第三(这是从@chepner的答案中得出的,我没有注意到),您必须确保变量在测试开始时处于已知状态

以下是应该采取的措施:

import unittest
from unittest import mock

import main  # importing the module lets you access the original global variable


class TestMain(unittest.TestCase):

    def setUp(self):
        main.feedback = False  # make sure the state is defined at test start

    @mock.patch('main.input')  # patch input to run the test w/o user interaction
    def test_setFeedback(self, mock_input):
        self.assertFalse(main.feedback)
        main.setFeedback('y')
        self.assertTrue(main.feedback)

相关问题 更多 >