From ccc4dacd1da3f4310f01af206268cc602fd5b543 Mon Sep 17 00:00:00 2001 From: Harald Nordgren Date: Sun, 23 Oct 2016 03:21:00 +0200 Subject: [PATCH] When using '--timer-top-n', output the '--timer-json-file' results with slowest tests first --- README.rst | 1 + nosetimer/plugin.py | 13 ++++++++++--- setup.py | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index bc5a602..a924ebd 100644 --- a/README.rst +++ b/README.rst @@ -143,3 +143,4 @@ Contributors - `@hugovk `_ - `@cgoldberg `_ - `@ereOn `_ +- `@HaraldNordgren `_ diff --git a/nosetimer/plugin.py b/nosetimer/plugin.py index 9b19d29..ef28e11 100644 --- a/nosetimer/plugin.py +++ b/nosetimer/plugin.py @@ -1,3 +1,4 @@ +import json import logging import os import re @@ -6,12 +7,17 @@ import timeit from nose.plugins import Plugin -# try to import Queue try: import Queue except ImportError: import queue as Queue +try: + from collections import OrderedDict +except ImportError: + from ordereddict import OrderedDict + + # define constants IS_NT = os.name == 'nt' @@ -149,10 +155,11 @@ class TimerPlugin(Plugin): d = sorted(self._timed_tests.items(), key=lambda item: item[1]['time'], reverse=True) + if self.json_file: - import json + dict_type = OrderedDict if self.timer_top_n else dict with open(self.json_file, 'w') as f: - json.dump({'tests': dict((k, v) for k, v in d)}, f) + json.dump({'tests': dict_type((k, v) for k, v in d)}, f) total_time = sum([vv['time'] for kk, vv in d]) diff --git a/setup.py b/setup.py index ec418db..1496625 100755 --- a/setup.py +++ b/setup.py @@ -20,12 +20,14 @@ setup( 'Stanislav Kudriashev', 'Raoul Snyman', 'Corey Goldberg', + 'Harald Nordgren', ]), url='https://github.com/mahmoudimus/nose-timer', packages=['nosetimer', ], install_requires=[ 'nose', 'termcolor', + 'ordereddict', ], license='MIT or BSD', entry_points={