From cc7f26b9478382861ceba3a859228bd61cc8c571 Mon Sep 17 00:00:00 2001 From: Mantas Date: Wed, 14 Jun 2017 11:35:32 +0300 Subject: [PATCH] Fix unorderable types error when sorting slow tests Part, where slow tests are printed, where not cevered with tests. Added tests for that. In rare cases, when time of two test cases are the same, sorted function lookes to the next element of tuple, and next element is test instance which is not orderable. And that rare case it gives following error: Traceback (most recent call last): File "CoverageTestRunner.py", line 312, in run() File "CoverageTestRunner.py", line 303, in run result = runner.run() File "CoverageTestRunner.py", line 265, in run for secs, test in sorted(result.timings)[-10:]: TypeError: unorderable types: FooTests() < FooTests() The fix was to always sort only by time, not by whole tuple. --- .gitignore | 1 + CoverageTestRunner.py | 6 ++++-- subdir/foo.py | 4 ++++ subdir/foo_tests.py | 12 ++++++++++-- testrun | 2 ++ 5 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/CoverageTestRunner.py b/CoverageTestRunner.py index a63b9c2..a69c0d2 100644 --- a/CoverageTestRunner.py +++ b/CoverageTestRunner.py @@ -23,6 +23,7 @@ import sys import time import logging +import operator __version__ = '1.11' @@ -258,11 +259,12 @@ def run(self): if result.missing_test_modules: print(len(result.missing_test_modules), "missing test modules") - maxtime = int(os.environ.get('COVERAGE_TEST_RUNNER_MAX_TIME', '10')) + maxtime = float(os.environ.get('COVERAGE_TEST_RUNNER_MAX_TIME', '10')) if end_time - start_time > maxtime: print() print("Slowest tests:") - for secs, test in sorted(result.timings)[-10:]: + timings = sorted(result.timings, key=operator.itemgetter(0)) + for secs, test in timings[-10:]: print(" %5.1f s %s" % (secs, str(test)[:70])) print("Time: %.1f s" % (end_time - start_time)) diff --git a/subdir/foo.py b/subdir/foo.py index 51a5dde..7a23f0f 100644 --- a/subdir/foo.py +++ b/subdir/foo.py @@ -13,4 +13,8 @@ def foo(self, a): time.sleep(0) return False + def slow(self, seconds): + import time + time.sleep(seconds) + foo = Foo() diff --git a/subdir/foo_tests.py b/subdir/foo_tests.py index a8a9740..cdc83bd 100644 --- a/subdir/foo_tests.py +++ b/subdir/foo_tests.py @@ -1,11 +1,19 @@ -import unittest, foo +import unittest +import foo + class FooTests(unittest.TestCase): def testTrue(self): f = foo.Foo() self.failUnlessEqual(f.foo(True), True) - + def testFalse(self): f = foo.Foo() self.failUnlessEqual(f.foo(False), False) + + +class SlowTests(unittest.TestCase): + + def testSlow(self): + foo.Foo().slow(0.11) diff --git a/testrun b/testrun index bf89868..2356af5 100755 --- a/testrun +++ b/testrun @@ -2,4 +2,6 @@ set -e +export COVERAGE_TEST_RUNNER_MAX_TIME=0.1 + python CoverageTestRunner.py subdir --ignore-missing-from=test-excluded