【问题标题】:How to generate a test report in python using selenium and unittest?如何使用 selenium 和 unittest 在 python 中生成测试报告?
【发布时间】:2020-12-03 14:58:24
【问题描述】:

我试图用不同的案例测试一个简单的登录表单。我使用 python、selenium、python 的 unittest 库进行测试,我可以通过 unittest 库完成测试,但是我如何为此生成测试报告?我尝试使用 HTMLTestRunner,但它有错误,我发现它是为 python 2x 版本编写的。很多库我们没有更新到 python 3x。我如何生成以下报告(为了不让它看起来很难看,我刚刚发布了两个案例): 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!

【问题讨论】:

    标签: python selenium unit-testing testing selenium-webdriver


    【解决方案1】:

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

    pip install HTMLTestRunner-Python3

    您可以在此处查看更新: https://pypi.org/project/HTMLTestRunner-Python3/0.8.0/

    另外,我认为您已经删除了从上面生成 html 报告的代码。仅供参考:

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

    【讨论】:

    • 它似乎工作。我使用的是旧版本的 htmltestrunner,新 python 有很多问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 2021-09-27
    相关资源
    最近更新 更多