5

I'm making unittests with python. I am not using any automatical test discovery. I am assembling TestCases into a TestSuite manually.

I can run these tests with unittest.TextTestRunner().run(suite), I would like to run them with unittest.main() so that I can use command line options (like -v/--failfast). The documentation says that unittest.main() can take a TestRunner option.

How to convert my TestSuite into a TestRunner?

Amandasaurus
  • 58,203
  • 71
  • 188
  • 248
  • 1
    TestSuite and TestRunner have **nothing** to do with each other. A suite is a collection of tests. A runner executes a collection of tests. You don't turn one into the other. You provide the suite to the runner (via `unittest.main()`, usually.) – S.Lott Sep 22 '10 at 19:04
  • How do I provide a testsuite to unittest.main()? Or how do I make a TestRunner that will run a given TestSuite (that I can pass to unittest.main()) – Amandasaurus Sep 22 '10 at 19:10
  • 1
    Are you just trying to pass `failfast` to your TestRunner? If you read the source, an undocumented feature of testrunner is that it accepts the `failfast` parameter. – S.Lott Sep 22 '10 at 19:17

2 Answers2

2

Near duplicate of How to run a testsuite with unittest.main (answer copy-and-pasted):

You can't pass a TestSuite to main, check out the constructor of unittest.main.TestProgram (which is was unittest.main actually is) and how this class works. The first argument if anything is the module name, not a testsuite.

main() actually takes its arguments from sys.argv, as it is actually intended to be used from the command line and not from within a program. It's just common to do so for convenience.

Community
  • 1
  • 1
Alex
  • 41,580
  • 88
  • 260
  • 469
-4

Do nothing except be sure you have this in your unit test module.

if __name__ == '__main__':
    unittest.main(failfast=True)

http://docs.python.org/library/unittest.html#unittest.main

From the documentation...

unittest.main( failfast=True, testRunner=unittest.TextTestRunner )
S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • I am not using unittest.main()'s test autodiscovery, I am hand crafting a TestSuite. If I use unittest.main as you suggest it won't run my tests. I've tried. – Amandasaurus Sep 22 '10 at 18:49
  • Why hand-cract? Why won't main autodiscover your tests? What are your test superclasses? What are your test names? It works perfectly for me. What are you doing wrong? – S.Lott Sep 22 '10 at 19:00
  • I am not using autodiscover because I am making TestCases based on known valid inputs for a function. I am using this approach http://stackoverflow.com/questions/2798956/python-unittest-generate-multiple-tests-programmatically/3772008#3772008 – Amandasaurus Sep 22 '10 at 19:11
  • @Rory: That approach seems silly. Why not put the loop in `runTest` where it's supposed to be? Why create a complex suite when you could move the complexity into one test case that simply iterates through the values? – S.Lott Sep 22 '10 at 19:22
  • @S.Lott see the benefits I listed in that answer. – Amandasaurus Sep 22 '10 at 19:38
  • @Rory: I did see those "benefits". I don't agree with them. They aren't "beneficial". This question -- indeed -- is evidence that it's actually harmful, not helpful. – S.Lott Sep 23 '10 at 00:51