Skip to content
Advertisement

How to generate a test report in python using selenium and unittest?

I was trying to test a simple login form with different cases. I used python,selenium,python’s unittest library for testing and i am able to get test done by unittest library but how can i generate test reports for this ? I tried with HTMLTestRunner but it has bugs and i found it was written for python 2x version. A lot of libraries we not updated to python 3x. How can i generate report for following (to not make it look ugly i just posted two cases): 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()

It’s test output:

============================= 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!

Advertisement

Answer

Did you try by installing htmltestrunner for python3. Use below command to install:

pip install HTMLTestRunner-Python3

You can see update here: https://pypi.org/project/HTMLTestRunner-Python3/0.8.0/

Also , i presume you have removed code to generate html report from above. I am putting for reference purpose:

if __name__ == "__main__":
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='..Reports'))
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement