From ad6c190b65c7bf8717aa88143a54f2bb2a66089e Mon Sep 17 00:00:00 2001 From: Marius Van Niekerk Date: Wed, 27 Dec 2017 15:52:09 -0500 Subject: [PATCH 1/4] Switch testing to pytest, Use pytest-benchmark so that regressions can be tracked in future. --- .travis.yml | 5 +++-- multipledispatch/tests/test_core.py | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index d8bfb01..8dc0148 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,13 +11,14 @@ python: install: - pip install coverage + - pip install pytest pytest-benchmark script: - | if [[ $(bc <<< "$TRAVIS_PYTHON_VERSION >= 3.3") -eq 1 ]]; then - nosetests --with-doctest + py.test --doctest-modules multipledispatch else - nosetests --with-doctest -I '.*_3only.py$' + py.test --doctest-modules --ignore=multipledispatch/tests/test_dispatcher_3only.py multipledispatch fi after_success: diff --git a/multipledispatch/tests/test_core.py b/multipledispatch/tests/test_core.py index 373ce99..69b8cdd 100644 --- a/multipledispatch/tests/test_core.py +++ b/multipledispatch/tests/test_core.py @@ -28,7 +28,7 @@ def f(x): assert raises(NotImplementedError, lambda: f('hello')) -def test_multipledispatch(): +def test_multipledispatch(benchmark): @dispatch(int, int) def f(x, y): return x + y @@ -203,3 +203,16 @@ def f(x, y): assert foo.f(A(), A()) == 1 assert foo.f(A(), C()) == 2 assert foo.f(C(), C()) == 2 + + +# Hacky method to get benchmnarks namespaced +class TestBenchMark(object): + pass + + +for k, v in list(locals().items()): + if callable(v) and (k.startswith('test_')): + + def bench(self, benchmark): + benchmark(v) + setattr(TestBenchMark, '{}'.format(k), bench) From 2e615f3eaf0f4e748001f0b892aad0f1330db1ec Mon Sep 17 00:00:00 2001 From: Marius Van Niekerk Date: Wed, 27 Dec 2017 16:38:39 -0500 Subject: [PATCH 2/4] travis ensure up to date py.test --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8dc0148..28ed962 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ python: install: - pip install coverage - - pip install pytest pytest-benchmark + - pip install --upgrade pytest pytest-benchmark script: - | From 6dd4cdb076fd3f128c79b7c42e1d32ef921dc0a2 Mon Sep 17 00:00:00 2001 From: Marius Van Niekerk Date: Wed, 27 Dec 2017 17:07:34 -0500 Subject: [PATCH 3/4] Move benchmarks to their own test file --- multipledispatch/tests/test_benchmark.py | 45 ++++++++++++++++++++++++ multipledispatch/tests/test_core.py | 13 ------- 2 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 multipledispatch/tests/test_benchmark.py diff --git a/multipledispatch/tests/test_benchmark.py b/multipledispatch/tests/test_benchmark.py new file mode 100644 index 0000000..3041ef0 --- /dev/null +++ b/multipledispatch/tests/test_benchmark.py @@ -0,0 +1,45 @@ +from multipledispatch import dispatch +import pytest + + +@dispatch(int) +def isint(x): + return True + + +@dispatch(object) +def isint(x): + return False + + +@dispatch(object, object) +def isint(x, y): + return False + + +@pytest.mark.parametrize("val", [1, 'a']) +def test_benchmark_call_single_dispatch(benchmark, val): + benchmark(isint, val) + + +@pytest.mark.parametrize("val", [(1, 4)]) +def test_benchmark_call_multiple_dispatch(benchmark, val): + benchmark(isint, *val) + + +def test_benchmark_add_and_use_instance(benchmark): + namespace = {} + + @benchmark + def inner(): + @dispatch(int, int, namespace=namespace) + def mul(x, y): + return x * y + + @dispatch(str, int, namespace=namespace) + def mul(x, y): + return x * y + + mul(4, 5) + mul('x', 5) + diff --git a/multipledispatch/tests/test_core.py b/multipledispatch/tests/test_core.py index 69b8cdd..d3f6eec 100644 --- a/multipledispatch/tests/test_core.py +++ b/multipledispatch/tests/test_core.py @@ -203,16 +203,3 @@ def f(x, y): assert foo.f(A(), A()) == 1 assert foo.f(A(), C()) == 2 assert foo.f(C(), C()) == 2 - - -# Hacky method to get benchmnarks namespaced -class TestBenchMark(object): - pass - - -for k, v in list(locals().items()): - if callable(v) and (k.startswith('test_')): - - def bench(self, benchmark): - benchmark(v) - setattr(TestBenchMark, '{}'.format(k), bench) From 17dfcbadad66d340fe2ae0f28503bcc5231a8d52 Mon Sep 17 00:00:00 2001 From: Marius Van Niekerk Date: Wed, 27 Dec 2017 18:56:15 -0500 Subject: [PATCH 4/4] pytest doesn't like very old versions of python --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 28ed962..0814116 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,13 @@ language: python python: - - "2.6" - "2.7" - - "3.2" - "3.3" - "3.4" - "3.5" - - "3.6-dev" + - "3.6" + - "3.7-dev" - "pypy" + - "pypy3" install: - pip install coverage