根据工作环境中pandas包的可用性,方法返回两个不同的输出:
pandas.DataFrame,如果大熊猫可用numpy.recarray对象.我该如何为这堂课写单元测试?
我能想到的一个解决方案是为两种情况编写测试(有和没有安装pandas)并相应地跳过测试,如下所示:
try:
import pandas
HAVE_PANDAS = True
except ImportError:
HAVE_PANDAS = False
import unittest
class TestClass(unittest.TestCase):
@unittest.skipUnless(HAVE_PANDAS, "requires pandas")
def tests_using_pandas(self):
# do something
@unittest.skipUnless(not HAVE_PANDAS, "doesn't require pandas")
def tests_without_pandas(self):
# do something
Run Code Online (Sandbox Code Playgroud)
但由于测试覆盖率和跳过测试的减少,我不太喜欢这个解决方案.我想对两种情况都进行测试.如果有人可以为此提出更好的替代解决方案,将会很有帮助.