我正在尝试为Python应用程序编写单元测试,我想编写一个单元测试来验证字典中的条目是否正确(基于操作系统)。
问题:我无法模仿我所在的操作系统。我只是想模拟操作系统,以便我可以验证字典的条目是否有效。我已经尝试过猴子修补,嘲弄和补丁,但目前没有任何作用(我不怀疑是由于我对单元测试的知识有限)。我现有的代码如下。任何(详细的)帮助,为什么这不起作用或我做错了什么/应该做的将非常感谢! :d
所以我有:
package.subpackage.module(mymodule.py)
import sys
if platform.system() == 'Linux':
mydictionary = {"1":one, "2":two, "3":three}
elif platform.system() == 'other_os':
mydictionary = {"A":aaa, "B":bee, "c":cee}
我在这里使用以下代码创建了一个单元测试:
package.subfolder.subsubfolder.module(myunittestmodule.py)
def test_dictionary():
import sys
sys.modules['platform'] = Mock()
import platform
targetPlatform = platform
targetPlatform.system = Mock(return_value="other_os")
from package.subpackage.module import mydictionary
for entry in mydictionary:
print(entry)
# for now I use print to see if this even works
# but it only returns the dictionary for my os (linux), not anything else :(