我有一个简单的方法,根据方法参数将全局变量设置为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()
你不需要嘲笑任何东西;在运行每个测试之前,您只需要确保全局变量处于已知状态。另外,使用
from main import *
在测试模块中创建一个名为feedback
的新的全局,与setFeedback
正在修改的main.feedback
不同你的测试有两个问题
首先,在
feedback
函数中使用input
,这将暂停测试,直到输入键为止。您可能应该模拟input
。也可以考虑调用^ {< CD1> }不属于^ {< CD5> }(参见@ CHEPENER注释)。其次,
from main import *
在这里不起作用(除了作为bad style),因为通过这种方式,您可以在测试模块中创建全局变量的副本-变量本身的更改不会传播到副本。您应该导入模块,以便访问模块中的变量第三(这是从@chepner的答案中得出的,我没有注意到),您必须确保变量在测试开始时处于已知状态
以下是应该采取的措施:
相关问题 更多 >
编程相关推荐