简体   繁体   English

当我运行测试套件时,出现TypeError错误,我无法理解为什么

[英]When I run my test suite I am getting a TypeError I am not able to understand why

I am using HtmlTestRunner to generate my test report, but I am not able to understand why I am getting this error: "TypeError: issubclass() arg 1 must be a class" 我正在使用HtmlTestRunner生成我的测试报告,但我无法理解为什么会收到此错误: "TypeError: issubclass() arg 1 must be a class"

My setup is Python 3.6, pytest, Ubuntu 17.10. 我的设置是Python 3.6,pytest,Ubuntu 17.10。

This is the code I have written: 这是我编写的代码:

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)

The test suite runner: 测试套件运行器:

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)

This is the resulting error: 这是导致的错误:

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

Your problem is that you are passing the test module, instead of the test class, to the runner. 您的问题是您正在将测试模块而不是测试类传递给运行程序。 If you look at unittest 's documentation for loadTestsFromTestCase , you'll see its argument needs to be a TestCase -derived class. 如果查看unittestloadTestsFromTestCase文档 ,您会发现其参数必须是TestCase派生的类。

I reduced your example to a minimal version to simplify my answer a bit: 我将您的示例简化为最小版本,以简化我的回答:

In tests.py : tests.py

import unittest

class MyTestCase(unittest.TestCase):
    def test_something(self):
        assert True

runner.py , based on yours: runner.py ,根据您的:

from unittest import TestLoader, TestSuite, TextTestRunner
import tests

example_tests = TestLoader().loadTestsFromTestCase(tests)
suite = TestSuite(example_tests)
runner = TextTestRunner()
runner.run(suite)

This will give the following output: 这将给出以下输出:

$ 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

If I switch runner.py to use the class instead, it works: 如果我将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)

Output: 输出:

$ python runner.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

You should be able to adapt this to your code. 您应该能够使其适应您的代码。 That being said, the pytest integration might not work with HtmlTestRunner, which is unittest-based. 话虽这么说,pytest集成可能无法与基于单元测试的HtmlTestRunner一起使用。 Make sure you look into pytest-html 确保您查看pytest-html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM