简体   繁体   中英

Python unittest - Extra Ran 0 tests in 0.000s

Here is my test script:

# tests/runner.py
import unittest

# import your test modules
import TC110
import TC112

# initialize the test suite
loader = unittest.TestLoader()
suite  = unittest.TestSuite()

# add tests to the test suite
suite.addTests(loader.loadTestsFromModule(TC110))
suite.addTests(loader.loadTestsFromModule(TC112))

# initialize a runner, pass it your suite and run it
runner = unittest.TextTestRunner(verbosity=3)
result = runner.run(suite)

I have TC110.py and TC112.py in the same directory and am running my test like this,

"python -m unittest runner"

I am getting output like this,

test_ldap_login (TC110.TestTC110) ... ok
test_download_artifact (TC112.TestTC112) ... ok

----------------------------------------------------------------------
Ran 2 tests in 1.929s

OK

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

Why I am getting "Ran 0 tests", how to get rid of this?

You're getting an extra "Ran 0 tests" for essentially the same reason that print(print("asdf")) prints an extra None : you're issuing two testing commands.

Your runner.py script loads tests from other files and runs them. If you had simply told Python to run the script ( python runner.py ), you wouldn't have gotten the spurious extra output.

Instead of telling Python to run the script, you have told the unittest module to load and run all tests from runner.py . As a side effect, this runs the body of runner.py , running the tests you wanted. unittest then loads and runs all 0 tests contained in runner.py , because you told it to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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