我使用HtmlTestRunner生成我的测试报告,但我无法理解为什么我收到此错误:"TypeError: issubclass() arg 1 must be a class"
我的设置是Python 3.6,pytest,Ubuntu 17.10。
这是我写的代码:
from pages.Home.category_page import CategoryPage
from utilites.testStatus import TestStatus
import pytest
import unittest
import time
@pytest.mark.usefixture("oneTimeSetUp","setUp")
class CategoryTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def classSetup(self,oneTimeSetUp):
self.ca = CategoryPage(self.driver)
self.ts = TestStatus(self.driver)
@pytest.mark.run(order=1)
def test_Announcements_link_WAF001(self):
result = self.ca.find_announcements_link()
self.ts.markFinal("Announcements link", result,"To find announcements link")
time.sleep(2)
@pytest.mark.run(order=2)
def test_FirstLinkInAnnouncements_WAF002(self):
result=self.ca.find_first_announcement_link()
self.ts.markFinal("Latest link in announcements",result,"To click on latest announcements link")
time.sleep(2)
@pytest.mark.run(order=3)
def test_Products_Link_WAF003(self):
result=self.ca.find_products()
self.ts.markFinal("Products link",result,"To find products link")
time.sleep(2)
@pytest.mark.run(order=4)
def test_FirstLinkInProducts_WAF004(self):
result=self.ca.find_first_products_link()
self.ts.markFinal("Latest link in products",result,"To click on latest products link")
time.sleep(2)
测试套件跑步者:
from unittest import TestLoader, TestSuite
from HtmlTestRunner import HTMLTestRunner
from tests.Home import category_test
example_tests = TestLoader().loadTestsFromTestCase(category_test)
suite = TestSuite(example_tests)
runner = HTMLTestRunner(output='example_suite', template='path/to/template', report_title='My Report')
runner.run(suite)
这是结果错误:
Traceback (most recent call last):
File "/home/manoj/PycharmProjects/untitled8/test/test_suite.py", line 6, in <module>
example_tests = TestLoader().loadTestsFromTestCase(to_test_login)
File "/usr/lib/python3.6/unittest/loader.py", line 85, in loadTestsFromTestCase
if issubclass(testCaseClass, suite.TestSuite):
TypeError: issubclass() arg 1 must be a class
您的问题是您将测试模块而不是测试类传递给跑步者。如果你看看unittest
's documentation for loadTestsFromTestCase
,你会发现它的论证需要是一个TestCase
派生类。
我将您的示例缩减为最小版本以简化我的回答:
在tests.py
:
import unittest
class MyTestCase(unittest.TestCase):
def test_something(self):
assert True
runner.py
,基于你的:
from unittest import TestLoader, TestSuite, TextTestRunner
import tests
example_tests = TestLoader().loadTestsFromTestCase(tests)
suite = TestSuite(example_tests)
runner = TextTestRunner()
runner.run(suite)
这将给出以下输出:
$ python runner.py
Traceback (most recent call last):
File "runner.py", line 4, in <module>
example_tests = TestLoader().loadTestsFromTestCase(tests)
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 85, in loadTestsFromTestCase
if issubclass(testCaseClass, suite.TestSuite):
TypeError: issubclass() arg 1 must be a class
如果我切换runner.py
来代替使用该类,它可以工作:
from unittest import TestLoader, TestSuite, TextTestRunner
from tests import MyTestCase
example_tests = TestLoader().loadTestsFromTestCase(MyTestCase)
suite = TestSuite(example_tests)
runner = TextTestRunner()
runner.run(suite)
输出:
$ python runner.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
您应该能够根据代码进行调整。话虽这么说,pytest集成可能不适用于基于单元测试的HtmlTestRunner。一定要看看pytest-html