1

[as I created test suite and generate a test report using HTMLTestRunner (Also modified little bit by me) single test run twice.] for that code (test suite) is:

import os
import time
import unittest
from xyz.base import log
from builtins import dir
from xyz.HTMLTestRunner import HTMLTestRunner

from xyz.tests.test.test_01 import TC01

class HTestSuite(unittest.TestCase):
    log.info('Running demo test suite')

    dir = os.getcwd()
    testLoad = unittest.TestLoader()
    log.info(dir)
    test_classes_to_run =[TC01]

    suites_list = []
    for test_class in test_classes_to_run:
        suite = testLoad.loadTestsFromTestCase(test_class)
        suites_list.append(suite)
    log.info(suites_list)
    newSuite = unittest.TestSuite(suites_list)
    log.info(newSuite.countTestCases())
    timestr = time.strftime("_%Y-%m-%d_%H.%M.%S")
    
    resultFile = open(os.path.join(dir, "TestReport"+ timestr + ".html"), "w")
    runner = HTMLTestRunner(stream=resultFile, title='test report', description='Tests Execution Report') 

    runner.run(newSuite)

if __name__ == "__main__":
    unittest.main()
Hollis HP
  • 11
  • 4
  • You have the test runner code inside the test case - so your test will be executed by `unittest.main`, and then again by your own testrunner. You probably have to replace `unittest.main` by your test runner code (and don't need `HTestSuite`, if I don't miss anything). – MrBean Bremen Jun 24 '20 at 13:41
  • can you @MrBeanBremen if I removed all the things no report is generated. plz provide any sample – Hollis HP Jun 30 '20 at 05:43
  • Ok, I just tested the code I had posted (e.g. moved the testrunner code under main and removed `unittest.main()` and the test class), and it works fine - runs the test once, and creates the report. How are you running the tests? I just used `python testrunner.py`. – MrBean Bremen Jul 02 '20 at 18:39
  • python -m unitest xyz\abc\def\demoTestSuite.py running the test suite and not generated any report.Also using IDE eclipse pydev to run test from IDE. – Hollis HP Jul 09 '20 at 12:33

1 Answers1

1

You have the test runner code inside the test case - so your test will be executed by unittest.main, and then again by your own testrunner. You can replace unittest.main by your test runner code (and don't need HTestSuite):

import os
import time
import unittest
from xyz.base import log
from builtins import dir
from xyz.HTMLTestRunner import HTMLTestRunner

from xyz.tests.test.test_01 import TC01

if __name__ == "__main__":
    log.info('Running demo test suite')

    dir = os.getcwd()
    testLoad = unittest.TestLoader()
    log.info(dir)
    test_classes_to_run =[TC01]

    suites_list = []
    for test_class in test_classes_to_run:
        suite = testLoad.loadTestsFromTestCase(test_class)
        suites_list.append(suite)
    log.info(suites_list)
    newSuite = unittest.TestSuite(suites_list)
    log.info(newSuite.countTestCases())
    timestr = time.strftime("_%Y-%m-%d_%H.%M.%S")
    
    resultFile = open(os.path.join(dir, "TestReport"+ timestr + ".html"), "w")
    runner = HTMLTestRunner(stream=resultFile, title='test report', description='Tests Execution Report') 

    runner.run(newSuite)

To run the tests, just use:

python xyz\abc\def\demoTestSuite.py
MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46