请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

运行TestMethod.py报错'TestMethod' object has no attribute 'case_name'

#coding:utf-8
import unittest
from Demo import RunMain
import json
import HTMLTestRunner
class TestMethod(unittest.TestCase):
def setUp(self):
self.run=RunMain()
def test_01(self):
url = ‘http://coding.imooc.com/api/cate

	data = {
		'timestamp': '1507006626132',
		'uid': '5249191',
		'uuid': '5ae7d1a22c82fb89c78f603420870ad7',
		'secret': '078474b41dd37ddd5efeb04aa591ec12',
		'token': '0b4c502ba647664be04dfedb32ad4f3d',
		'cid': '0'
	}

	res=self.run.run_main(url,'POST',data)
	self.assertEqual(res['errorCode'],1007,'测试失败')
	print('这是第一个case')

#@unittest.skip('test_02')
def test_02(self):

	url = 'http://coding.imooc.com/api/cate'

	data = {
		'timestamp': '1507006626132',
		'uid': '5249191',
		'uuid': '5ae7d1a22c82fb89c78f603420870ad7',
		'secret': '078474b41dd37ddd5efeb04aa591ec12',
		'token': '0b4c502ba647664be04dfedb32ad4f3d',
		'cid': '0'
	}

	res=self.run.run_main(url,'POST',data)
	self.assertEqual(res['errorCode'], 1007, '测试失败')
	print('这是第二个case')

if name==‘main’:
suite = unittest.TestSuite()
suite.addTest(TestMethod(‘test_01’))
suite.addTest(TestMethod(‘test_02’))
filepath="D:\programs\report\htmlreport.html"
fp=open(filepath,‘wb’)
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=‘this is first report’,description=‘Report_description’)
runner.run(suite)
fp.close()

运行上段代码报错

…Traceback (most recent call last):
File “D:/programs/ImoocInterface/base/test_method.py”, line 51, in
runner.run(suite)
File “D:\programs\lib\HTMLTestRunner.py”, line 629, in run
self.generateReport(test, result)
File “D:\programs\lib\HTMLTestRunner.py”, line 676, in generateReport
report = self._generate_report(result)
File “D:\programs\lib\HTMLTestRunner.py”, line 727, in _generate_report
description = cls_results[0][1].case_name
AttributeError: ‘TestMethod’ object has no attribute ‘case_name’

这个case_name应该在哪里添加?
图片描述

正在回答 回答被采纳积分+3

1回答

Mushishi 2020-02-11 10:46:41
# coding=utf-8
import unittest
import HTMLTestRunner
 
class TestStudy(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("this is class method")
 
    def setUp(self):
        print("this is setup")
 
    def test_one(self):
        # flag = False
        print("this is test01")
        self.assertEqual('1', '2', '数据错误')
 
 
    def test_two(self):
        print("this is test02")
 
    def tearDown(self):
        print("this is teardown")
 
    @classmethod
    def tearDownClass(cls):
        print("this is tearDownClass")
 
if __name__ == '__main__':
 
    suite = unittest.TestSuite()
    suite.addTest(TestStudy("test_one"))
    suite.addTest(TestStudy("test_two"))
    html_file = r"D:/AppTest/report/report.html"
    fp = open(html_file, "wb")
    HTMLTestRunner.HTMLTestRunner(stream=fp).run(suite)

你这样看是否可以。case name不需要 他就是test_one 

0 回复 有任何疑惑可以回复我~
  • 提问者 qq_莯莯_0 #1
    不行的哦,我将你的代码改了html的路径,还是报一样的错误
    
    
    
    D:\programs\python.exe D:/programs/ImoocInterface/base/answer.py
    this is class method
    this is tearDownClass
    F.Traceback (most recent call last):
      File "D:/programs/ImoocInterface/base/answer.py", line 36, in <module>
        HTMLTestRunner.HTMLTestRunner(stream=fp).run(suite)
      File "D:\programs\lib\HTMLTestRunner.py", line 629, in run
        self.generateReport(test, result)
      File "D:\programs\lib\HTMLTestRunner.py", line 676, in generateReport
        report = self._generate_report(result)
      File "D:\programs\lib\HTMLTestRunner.py", line 727, in _generate_report
        description = cls_results[0][1].case_name
    AttributeError: 'TestStudy' object has no attribute 'case_name'
    回复 有任何疑惑可以回复我~ 2020-02-11 22:29:18
  • Mushishi 回复 提问者 qq_莯莯_0 #2
    你deHTMLTestRunner哪里的?python什么版本?
    回复 有任何疑惑可以回复我~ 2020-02-14 23:19:24
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信