From 62974f675d9690bad5543b95bbd224fa5b95910d Mon Sep 17 00:00:00 2001 From: Leo Hanisch Date: Sat, 14 Nov 2020 13:13:07 +0100 Subject: [PATCH] setup pytest added first unit tests --- .github/pull_request_template.md | 7 +-- .travis.yml | 4 +- .vscode/settings.json | 7 ++- pytest.ini | 2 + requirements-dev.txt | 5 ++ tests/__init__.py | 4 ++ tests/util/__init__.py | 4 ++ tests/util/coordinate_test.py | 81 ++++++++++++++++++++++++++++++++ 8 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 pytest.ini create mode 100644 tests/__init__.py create mode 100644 tests/util/__init__.py create mode 100644 tests/util/coordinate_test.py diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index efa760b..66f49bd 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,9 +1,3 @@ ---- -name: Pull Request Template -about: Create a pull request to resolve an issue -assignees: 'HaaLeo' ---- - # Summary @@ -13,3 +7,4 @@ assignees: 'HaaLeo' # Related Issues +Fixes # diff --git a/.travis.yml b/.travis.yml index 5761116..d9984e8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,9 @@ stages: install: - pip install -r requirements-dev.txt -script: pylint swarmlib +script: + - pylint swarmlib tests + - pytest tests jobs: include: diff --git a/.vscode/settings.json b/.vscode/settings.json index 306a452..71f2fe1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -24,6 +24,7 @@ "pylint", "pypi", "pyplot", + "pytest", "qsize", "swarmlib", "wfunc" @@ -33,11 +34,15 @@ "files.trimFinalNewlines": true, "python.linting.enabled": true, "python.linting.pylintEnabled": true, + "python.testing.pytestEnabled": true, "python.pythonPath": ".venv/bin/python", "colorInfo.languages": [ { "selector": "python", "colors": "hex" } - ] + ], + "files.associations": { + ".pylintrc": "ini" + } } diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..c0017ea --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +addopts = --spec --cov swarmlib diff --git a/requirements-dev.txt b/requirements-dev.txt index c0d074c..ba44615 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,5 +1,10 @@ -r requirements.txt +pytest +pytest-describe +pytest-spec +pytest-cov +autopep8 pylint pylint-quotes pylintfileheader diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..28f0ec5 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,4 @@ +# ------------------------------------------------------------------------------------------------------ +# Copyright (c) Leo Hanisch. All rights reserved. +# Licensed under the BSD 3-Clause License. See LICENSE.txt in the project root for license information. +# ------------------------------------------------------------------------------------------------------ diff --git a/tests/util/__init__.py b/tests/util/__init__.py new file mode 100644 index 0000000..28f0ec5 --- /dev/null +++ b/tests/util/__init__.py @@ -0,0 +1,4 @@ +# ------------------------------------------------------------------------------------------------------ +# Copyright (c) Leo Hanisch. All rights reserved. +# Licensed under the BSD 3-Clause License. See LICENSE.txt in the project root for license information. +# ------------------------------------------------------------------------------------------------------ diff --git a/tests/util/coordinate_test.py b/tests/util/coordinate_test.py new file mode 100644 index 0000000..c5d06d4 --- /dev/null +++ b/tests/util/coordinate_test.py @@ -0,0 +1,81 @@ +# ------------------------------------------------------------------------------------------------------ +# Copyright (c) Leo Hanisch. All rights reserved. +# Licensed under the BSD 3-Clause License. See LICENSE.txt in the project root for license information. +# ------------------------------------------------------------------------------------------------------ + +import numpy as np + + +import pytest + +from swarmlib.util.coordinate import Coordinate + +# pylint: disable=unused-variable + + +@pytest.fixture +def test_func(): + return lambda x: np.sum(x) # pylint: disable=unnecessary-lambda + + +@pytest.fixture +def test_object(test_func): + return Coordinate( + function=test_func, + bit_generator=np.random.default_rng(3), + lower_boundary=0.1, + upper_boundary=3.9) + + +def describe_coordinate(): + def describe_constructor(): + def describe_raise_error(): + def if_bit_generator_missing(): + with pytest.raises(KeyError): + Coordinate(function='foo') + + def if_function_is_missing(): + with pytest.raises(KeyError): + Coordinate(bit_generator='foo') + + def initializes_correctly(test_object): + np.testing.assert_array_equal(test_object.position, [0.42546683514577255, 0.9998799250651788]) + np.testing.assert_equal(test_object.value, 1.4253467602109513) + np.testing.assert_array_less(test_object.position, 3.9) + np.testing.assert_array_less(0.1, test_object.position) + + def describe_comparison(): + @pytest.fixture + def other(test_func): + return Coordinate( + function=test_func, + bit_generator=np.random.default_rng(4), + lower_boundary=0.1, + upper_boundary=3.9) + + def equal(test_object): + assert test_object == test_object # pylint: disable=comparison-with-itself + + def not_equal(test_object, other): + assert test_object != other + + def less(test_object, other): + assert test_object < other + + def less_equal(test_object, other): + assert test_object <= other + assert test_object <= test_object # pylint: disable=comparison-with-itself + + def greater(test_object, other): + assert other > test_object + + def greater_equal(test_object, other): + assert other >= test_object + assert test_object >= test_object # pylint: disable=comparison-with-itself + + def describe_position(): + def setter_clips_position_and_updates_value(test_object): + test_object._position = [-5, 7] # pylint: disable=protected-access + + np.testing.assert_array_equal(test_object.position, [0.1, 3.9]) + np.testing.assert_equal(test_object.value, 4)