#导包 import unittest from tools.HTMLTestRunner import HTMLTestRunner from script.script import test_teacher_sys #封装测试套件 suite = unittest.TestSuite suite.addTest(unittest.makeSuite(test_teacher_sys)) #指定测试报告位置 report = './report/登录接口测试报告.html' #文件流形式打开测试报告 with open('登录接口测试报告.html',"wb") as f: #创建htmltesteunner的运行器 runner = HTMLTestRunner(f, title='登录接口测试报告')报错详情Traceback (most recent call last): File "D:\pythonProject1\teachersysterm\run_suite.py", line 4, in <module> from tools.HTMLTestRunner import HTMLTestRunner File "D:\pythonProject1\teachersysterm\tools\HTMLTestRunner.py", line 631 print(sys.stderr, ‘\nTime Elapsed: %s’ % (self.stopTime-self.startTime)) ^ SyntaxError: invalid character '‘' (U+2018) 进程已结束,退出代码1,为什么报错,怎样解决
时间: 2024-02-25 15:54:48 浏览: 215
报错信息显示是语法错误,具体原因是在HTMLTestRunner.py文件的第631行中,使用了非英文的单引号(‘),而不是英文的单引号(')。建议把该行代码修改为使用英文的单引号,如下所示:
```
print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))
```
修改后再运行代码,应该就可以正常执行了。
相关问题
#导包 import requests from api.teacherAPI import teacher_sys import unittest #创建测试类 class test_teacher_sys(unittest.TestCase): def setUp(self): #实例化session self.session = requests.session() self.login = teacher_sys.login() def tearDown(self): #判断,如果session存在就关闭 if self.session: self.session.close() def test01_login_success(self): response = self.login(self.session,"guest","guest") self.assertEqual(200,response.status_code)错误详情Error Traceback (most recent call last): File "D:\pythonProject1\teachersysterm\script\script.py", line 11, in setUp self.login = teacher_sys.login() TypeError: teacher_sys.login() missing 3 required positional arguments: 'self', 'username', and 'password',为什么出现这个错误,怎样解决
这个错误提示是因为 `teacher_sys.login()` 方法需要传入三个参数,分别是 `self`、`username` 和 `password`,但是在测试类中实例化 `self.login = teacher_sys.login()` 时没有传入这三个参数。因此,你需要修改测试类中的 `setUp()` 方法,将 `self.login` 实例化时传入正确的参数,例如:
```
class test_teacher_sys(unittest.TestCase):
def setUp(self):
# 实例化session
self.session = requests.session()
# 实例化登录方法,传入正确的参数
self.login = teacher_sys.login(self.session, "guest", "guest")
def tearDown(self):
# 判断,如果session存在就关闭
if self.session:
self.session.close()
def test01_login_success(self):
response = self.login()
self.assertEqual(200, response.status_code)
```
这样就可以正确地实例化 `self.login` 并调用登录方法了。
if __name__ == '__main__': suite = unittest.TestSuite() runner = unittest.TextTestRunner() suite.addTest(TestDemo('test_case_01')) # suite.addTest(TestDemo('test_case_02')) runner.run(suite) with open("test_report.html", "w") as f: runner = HTMLTestRunner.HTMLTestRunner(tream=f, title='Test Report', description='This is a test report generated by web') runner.run(suite) 这段代码有错误吗?
代码存在一个小错误,HTMLTestRunner 的参数名应该是 `stream` 而不是 `tream`。正确的代码如下:
```python
if __name__ == '__main__':
suite = unittest.TestSuite()
runner = unittest.TextTestRunner()
suite.addTest(TestDemo('test_case_01'))
# suite.addTest(TestDemo('test_case_02'))
with open("test_report.html", "w") as f:
runner = HTMLTestRunner.HTMLTestRunner(stream=f, title='Test Report', description='This is a test report generated by web')
runner.run(suite)
```
另外,如果要生成 HTML 报告,需要先安装 HTMLTestRunner 库。可以通过以下命令进行安装:
```
pip install html-testRunner
```
阅读全文