0
我在过去的6个小时里阅读过很多文章,但我仍然不明白模拟和单元测试。我想单元测试一个打开的函数,我如何正确地做到这一点?如何进行单元测试和模拟开放功能
我也担心我的大部分代码使用外部文件进行数据导入和操作。我明白我需要嘲笑他们进行测试,但我正在努力理解如何前进。
请多多指教。谢谢你在前进
prototype5.py
import os
import sys
import io
import pandas
pandas.set_option('display.width', None)
def openSetupConfig (a):
"""
SUMMARY
Read setup file
setup file will ONLY hold the file path of the working directory
:param a: str
:return: contents of the file stored as str
"""
try:
setupConfig = open(a, "r")
return setupConfig.read()
except Exception as ve:
ve = (str(ve) + "\n\nPlease ensure setup file " + str(a) + " is available")
sys.exit(ve)
dirPath = openSetupConfig("Setup.dat")
test_prototype5.py
import prototype5
import unittest
class TEST_openSetupConfig (unittest.TestCase):
"""
Test the openSetupConfig function from the prototype 5 library
"""
def test_open_correct_file(self):
result = prototype5.openSetupConfig("Setup.dat")
self.assertTrue(result)
if __name__ == '__main__':
unittest.main()