CKTIS / Python
unittest.main
Discovers tests from a module and runs them using a configured runner.
Version: Python 3.14; repository image, production runtime not attested
Syntax
unittest.main(module='__main__', argv=['demo'], exit=False)Discovers tests from a module and runs them using a configured runner.
Disabling process exit returns a program object. Passing a controlled argument list prevents unrelated command-line flags from changing discovery.
The example uses a single module and supplies argv with a program name followed by no test selectors, so both discovery and output are controlled. A dotted selector chooses one test. exit=True normally raises SystemExit with a status code. With exit=False inspect program.result. Here one test ran and succeeded.
import unittest,types,io
m=types.ModuleType('suite')
class Sample(unittest.TestCase):
def test_one(self): pass
m.Sample=Sample
def run(args,exit):
runner=unittest.TextTestRunner(stream=io.StringIO())
return unittest.main(module=m,argv=['demo',*args],testRunner=runner,exit=exit)
assert run(['Sample.test_one'],False).result.testsRun==1
try: run([],True)
except SystemExit as exc: assert exc.code==0
else: raise AssertionError
Examples
import unittest
import io
class Check(unittest.TestCase):
def test_total(self): self.assertEqual(2 + 3, 5)
runner = unittest.TextTestRunner(stream=io.StringIO())
p = unittest.main(argv=["demo"], testRunner=runner, exit=False)
print(p.result.testsRun, p.result.wasSuccessful())
Result
1 True
The example uses a single module and supplies argv with a program name followed by no test selectors, so both discovery and output are controlled. A dotted selector chooses one test. exit=True normally raises SystemExit with a status code. With exit=False inspect program.result. Here one test ran and succeeded.