如何使用selenium和unittest在python中生成测试报告?

如何使用selenium和unittest在python中生成测试报告?,python,selenium,unit-testing,testing,selenium-webdriver,Python,Selenium,Unit Testing,Testing,Selenium Webdriver,我试图用不同的案例测试一个简单的登录表单。我使用python、selenium和python的unittest库进行测试,我能够通过unittest库完成测试,但如何生成测试报告呢?我试过使用HTMLTestRunner,但它有bug,我发现它是为python 2x版本编写的。很多库我们没有更新到python3x。我如何为以下内容生成报告(为了不让它看起来难看,我刚刚发布了两个案例): My_code.py import unittest from selenium import

我试图用不同的案例测试一个简单的登录表单。我使用python、selenium和python的unittest库进行测试,我能够通过unittest库完成测试,但如何生成测试报告呢?我试过使用HTMLTestRunner,但它有bug,我发现它是为python 2x版本编写的。很多库我们没有更新到python3x。我如何为以下内容生成报告(为了不让它看起来难看,我刚刚发布了两个案例): My_code.py

    import unittest
    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    import time
    
    class Pytest(unittest.TestCase):
    
        def setUp(self):
            self.driver = webdriver.Firefox()
    
        def test_login(self):
            driver = self.driver
            driver.get(url)
    
            WebDriverWait(driver, 100).until(EC.element_to_be_clickable((By.CLASS_NAME, 'btn-gradient-primary'))).click()
            emailFieldError = driver.find_element_by_id('username-error').text
            passFieldError = driver.find_element_by_id('password-error').text
            if emailFieldError == expectedErrorEmail and passFieldError == expectedErrorPassword:
                print(f'Case-1 worked on email and password fields. Error appeared {emailFieldError},{passFieldError}')
            else:
                False
        def test_login1(self):
            driver = self.driver
            driver.get(url)
            driver.find_element_by_id('username').send_keys(email)
            driver.find_element_by_class_name('btn-gradient-primary').click()
            time.sleep(1)
            passFieldError = driver.find_element_by_id('password-error').text
            if passFieldError == expectedErrorPassword:
                print(f'Case-2 worked, Email given but not password. Error appeared {passFieldError}')
            else:
                False
        def tearDown(self):
            self.driver.close()
    if __name__ == "__main__":
        unittest.main()
它的测试输出:

============================= test session starts ==============================
platform linux -- Python 3.7.6, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 -- /home/preetham/anaconda3/bin/python
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/home/preetham/Downloads/other projects/affiliate bot/.hypothesis/examples')
rootdir: /home/preetham/Downloads/other projects/affiliate bot
plugins: doctestplus-0.5.0, hypothesis-5.5.4, dash-1.12.0, openfiles-0.4.0, arraydiff-0.3, remotedata-0.3.2, astropy-header-0.1.2
collecting ... collected 8 items

connectdb.py::Pytest::test_login 
connectdb.py::Pytest::test_login1 
connectdb.py::Pytest::test_login2 
connectdb.py::Pytest::test_login3 
connectdb.py::Pytest::test_login4 
connectdb.py::Pytest::test_login5 
connectdb.py::Pytest::test_login6 
connectdb.py::Pytest::test_login7 

======================== 8 passed in 102.47s (0:01:42) =========================

Process finished with exit code 0
PASSED                                  [ 12%]Case-1 worked on email and password fields. Error appeared Username is required.,Password is required.
PASSED                                 [ 25%]Case-2 worked, Email given but not password. Error appeared Password is required.
PASSED                                 [ 37%]Case-3 worked, Password given but not email. Error appeared Username is required.
PASSED                                 [ 50%]Case-4 worked, Both given but email is wrong. Error appeared Please check login credentials
PASSED                                 [ 62%]Case-5 worked, Both given but email is wrong. Error appeared Please check login credentials
PASSED                                 [ 75%]Case-6 worked, Both given but email is invalid. Error appeared Please enter a valid email address.
PASSED                                 [ 87%]Case-7 worked, Both given email are correct. Message appeared Please check login credentials
PASSED                                 [100%]Case-8 worked, Both given email are correct. Message appeared Success!

您是否尝试为python3安装htmltestrunner。使用以下命令安装:

pip安装HTMLTStrunner-Python3

您可以在此处看到更新:

另外,我假定您已经删除了从上面生成html报告的代码。我提出以下建议以供参考:

if __name__ == "__main__":
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='..\Reports'))

它似乎起作用了。我使用了旧版本的htmltestrunner,它与新的python有很多问题。