Python unittest正确设置全局变量

标签 python python-3.x unit-testing python-unittest python-unittest.mock

我有一个简单的方法,可以根据方法参数将全局变量设置为 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 环境还很陌生,所以我不知道自己做错了什么。

我已经在这里阅读了一篇关于 mocking 的文章,但是由于我的方法更改了一个全局变量,我不知道 mocking 是否可以解决这个问题。

我将不胜感激。

这是代码:

主要.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()

最佳答案

你的测试有两个问题。

首先,您使用 input在您的 feedback功能,这将停止测试,直到您输入一个键。你可能应该模拟 input .您也可以考虑调用 input不属于 setFeedback (见@chepner 的评论)。

二、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)

关于Python unittest正确设置全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61540156/

相关文章:

python-3.x - 如何在 PyQt 中更改 QCombobox 突出显示颜色

ios - 使用 Swift 在 Xcode 中进行异步 UI 测试

javascript - 您将如何测试此路由代码?

python - Process using multiprocessing 没有输出

python - SWIG 包装 C 库引发异常的最优雅方式

python-3.x - 克隆前和克隆后的 keras 模型表现不同

python - 将字符串添加到线程名称末尾

python - 交互式地改变 Bokeh 图中的字形

python - Subprocess.popen - 从属写入失败 : Broken pipe; aborting

c# - 从容器测试接口(interface)是好是坏?