HTML 测试运行程序即使在导入 stringIO 后也出现导入错误



嗨,我使用以下代码在我的代码中实现 HTML 测试运行器:

import HtmlTestRunner
import unittest
from io import StringIO
class TestStringMethods(unittest.TestCase):
""" Example test for HtmlRunner. """
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
def test_error(self):
""" This test should be marked as error one. """
raise ValueError
def test_fail(self):
""" This test should fail. """
self.assertEqual(1, 2)
@unittest.skip("This is a skipped test.")
def test_skip(self):
""" This test should be skipped. """
pass
if __name__ == '__main__':
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='test_dir'))

但收到以下错误:

C:\Users\inswadhwa\AppData\Local\Programs\Python\Python36-32 \python.exe C:/Users/inswadhwa/PycharmProjects/automation/assertion.py 回溯(最近最后一次调用(:文件 "C:/Users/inswadhwa/PycharmProjects/automation/assertion.py" ,第 2 行,导入 HTMLTestRunner 文件 "C:\

Users\inswadhwa\AppData\Local\Programs\Python\Python36-3 2\lib\HTMLTestRunner .py",第 97 行,导入中 StringIO 模块未发现错误:没有名为"StringIO"的模块 进程已完成,退出代码为 1

我已经导入了StringIO.

任何人都可以提出克服这个问题的方法吗?

看起来你正在使用这个库,它似乎没有更新为与 Python 3 一起使用,其中旧的StringIO模块已被替换为io.StringIO类。

请尝试改用html-testRunner。它应该适用于Python 3。

相关内容

最新更新