Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setup pytest added first unit tests #25

Merged
merged 1 commit into from
Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
---
name: Pull Request Template
about: Create a pull request to resolve an issue
assignees: 'HaaLeo'
---

# Summary
<!-- Please give a brief summary about what changes with this pull request -->

Expand All @@ -13,3 +7,4 @@ assignees: 'HaaLeo'

# Related Issues
<!-- Mention the issue this PR relates to. E.g.: Fixes #1 -->
Fixes #
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ stages:
install:
- pip install -r requirements-dev.txt

script: pylint swarmlib
script:
- pylint swarmlib tests
- pytest tests

jobs:
include:
Expand Down
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"pylint",
"pypi",
"pyplot",
"pytest",
"qsize",
"swarmlib",
"wfunc"
Expand All @@ -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"
}
}
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
addopts = --spec --cov swarmlib
5 changes: 5 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
-r requirements.txt

pytest
pytest-describe
pytest-spec
pytest-cov
autopep8
pylint
pylint-quotes
pylintfileheader
Expand Down
4 changes: 4 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
# ------------------------------------------------------------------------------------------------------
4 changes: 4 additions & 0 deletions tests/util/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
# ------------------------------------------------------------------------------------------------------
81 changes: 81 additions & 0 deletions tests/util/coordinate_test.py
Original file line number Diff line number Diff line change
@@ -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)