就是这样写的re = TextTestRunner().run(suite) br = BeautifulReport(re) try: br.report("离线上位机_设备管理",filename="devicemanagement.html", report_dir="Test_report") except Exception as e: print(e) 报错'TextTestResult' object has no attribute 'run'
时间: 2024-03-19 07:46:29 浏览: 18
根据您提供的代码,我认为问题可能在于`TextTestRunner()`类的导入或引用方式。请确认您在代码的顶部是否正确导入了`TextTestRunner`类,例如:
```python
import unittest
from unittest.runner import TextTestRunner
from BeautifulReport import BeautifulReport
# ...其他代码
runner = TextTestRunner()
test_result = runner.run(suite)
br = BeautifulReport(test_result)
# ...其他代码
```
如果您已经正确导入了`TextTestRunner`类并且仍然遇到相同的错误,请提供完整的错误信息和代码,以便更好地帮助您解决问题。
相关问题
suite = unittest.TestSuite() suite.addTest(DeviceManagement_testcases.TestConnect('test01_connect')) suite.addTest(DeviceManagement_testcases.TestConnect('test02_disconnect')) result = TextTestRunner().run(suite) br = BeautifulReport(result) br.report("离线上位机_设备管理",filename="devicemanagement.html", report_dir="Test_report")执行提示AttributeError: 'TextTestResult' object has no attribute 'run'
根据您提供的代码,您首先使用unittest.TestSuite()创建了测试套件,然后使用suite.addTest()方法向测试套件中添加了两个测试用例。接下来,您使用TextTestRunner().run()方法运行了测试套件,并将测试结果保存到result变量中。最后,您使用BeautifulReport的report()方法来生成HTML报告,并将result作为参数传递给BeautifulReport。
根据错误提示,问题是由于TextTestResult对象没有run属性导致的。TextTestResult是unittest框架中的测试结果对象,它只能用于生成文本报告,而不能用于BeautifulReport生成HTML报告。因此,您需要将result变量改为unittest.TestResult()对象,并重新运行测试用例。以下是修改后的示例代码:
```python
import unittest
from BeautifulReport import BeautifulReport
class TestConnect(unittest.TestCase):
def test01_connect(self):
self.assertEqual(1, 1)
def test02_disconnect(self):
self.assertEqual(2, 2)
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(TestConnect('test01_connect'))
suite.addTest(TestConnect('test02_disconnect'))
result = unittest.TestResult()
suite.run(result)
br = BeautifulReport(suite)
br.report("离线上位机_设备管理",filename="devicemanagement.html", report_dir="Test_report")
```
在上面的示例代码中,我们将result变量改为unittest.TestResult()对象,并使用suite.run(result)方法运行测试套件。然后,我们将suite对象传递给BeautifulReport,并调用report()方法来生成HTML报告。
请注意,我们还将测试用例从DeviceManagement_testcases.py文件中移动到了同一文件中,以便更好地演示代码。您可以将其改为您自己的测试用例文件。
如果您仍然无法生成HTML网页报告,请提供更多的错误信息,以便我能够更好地帮助您解决问题。
result = unittest.TestResult() suite.run(result) report = BeautifulReport(result) 这样写正确吗
这段代码的逻辑是:
1. 创建一个`TestResult`实例`result`,用于存储测试结果。
2. 运行测试套件`suite`并将结果存储在`result`中。
3. 使用`result`生成测试报告。
其中,第1步和第2步是正确的。但是,第3步存在问题。`BeautifulReport`是一个第三方库,它提供了生成HTML测试报告的功能。但是,它的构造函数需要一个`unittest.TestSuite`实例作为参数,而不是`unittest.TestResult`实例。
因此,你需要将代码修改为:
```
import unittest
from BeautifulReport import BeautifulReport
# 定义测试用例
class MyTestCase(unittest.TestCase):
def test_something(self):
self.assertEqual(1 + 1, 2)
# 定义测试套件
suite = unittest.TestSuite()
suite.addTest(MyTestCase('test_something'))
# 运行测试套件并获取测试结果
result = unittest.TestResult()
suite.run(result)
# 使用测试结果生成测试报告
report = BeautifulReport(suite)
report.report(description='My Test Report', filename='my_report.html')
```
在上面的例子中,我们首先定义了一个测试用例`MyTestCase`,并将其添加到测试套件中。然后,我们创建了一个`TestResult`实例`result`,并使用测试套件的`run()`方法运行测试并将结果存储在`result`中。最后,我们使用`BeautifulReport`生成测试报告,并将测试套件`suite`作为参数传递给它。这样,我们就可以使用测试结果生成测试报告了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)