[TIP] runTest?

Chris Jerdonek chris.jerdonek at gmail.com
Tue Aug 27 13:33:48 PDT 2019

Why does each of your classes have a runTest method in the first place?
What is its purpose —- what does it do?


On Tue, Aug 27, 2019 at 12:51 PM Dan Stromberg <drsalists at gmail.com> wrote:

> Hi folks.
> I'm working on a large Python 3/Python 2 codebase that has several
> automated test cases and test classes.  I know Python 2 is going away soon.
> We have lots of test* methods in classes that inherit
> from unittest.TestCase. Each of those classes also provides a runTest
> method.
> It's bugging me that the runTest method is getting used, and the test*
> methods are getting ignored, unless we call the test* methods from the
> runTest method.
> In fact, if I rename the runTest method from a test class to something
> unused, I get a traceback:
> runTest (tests.test_disposable_locks.TestDisposableLocks)
> No test ... Traceback (most recent call last):
>   File "test_runner.py", line 98, in <module>
>     sys.exit(main())
>   File "test_runner.py", line 90, in main
>     result = runner.run(test_suite)
>   File "/usr/lib/python3.6/unittest/runner.py", line 176, in run
>     test(result)
>   File "/usr/lib/python3.6/unittest/suite.py", line 84, in __call__
>     return self.run(*args, **kwds)
>   File "/usr/lib/python3.6/unittest/suite.py", line 122, in run
>     test(result)
>   File "/usr/lib/python3.6/unittest/case.py", line 653, in __call__
>     return self.run(*args, **kwds)
>   File "/usr/lib/python3.6/unittest/case.py", line 580, in run
>     testMethod = getattr(self, self._testMethodName)
> AttributeError: 'TestDisposableLocks' object has no attribute 'runTest'
> I've been pulling my hair out trying to figure out why unittest is
> preferring runTest over test*.  Isn't unittest supposed to fall back on
> runTest only if there are no test* methods?
> Or do I need something like:
> suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)
> (from https://docs.python.org/2/library/unittest.html)
> ?  I tried getting this going with pytest a while back, but eventually set
> the project aside a while - maybe that was where I got the idea that
> runTest should be ignored if test* methods are present....
> I still hope to use pytest with this code at some point, but for now I'd
> be happy if the unittest test discovery just worked the way I expect.  My
> main goals for switching to pytest is to parallelise the tests, and to stop
> having to list "important" tests in a runTest method lest they be ignored.
> Also, I'm finding that sometimes raising an exception terminates the
> testing for the entire suite, rather than leading to a failed/errored test
> and continuing, presumably because "runTest" is "the test", so if it errors
> out anywhere, that's it for "that test".  If pytest can help with that,
> that'd be awesome.
> What can I do to get runTest out of the mix when test* methods are present?
> Thanks!
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20190827/f0282957/attachment.htm>

More information about the testing-in-python mailing list