Skip to content

Commit a507f12

Browse files
authored
Merge pull request #25 from HaaLeo/engineering/setup-pytest
setup pytest added first unit tests
2 parents b11cb92 + 62974f6 commit a507f12

File tree

8 files changed

+106
-8
lines changed

8 files changed

+106
-8
lines changed

.github/pull_request_template.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
---
2-
name: Pull Request Template
3-
about: Create a pull request to resolve an issue
4-
assignees: 'HaaLeo'
5-
---
6-
71
# Summary
82
<!-- Please give a brief summary about what changes with this pull request -->
93

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

148
# Related Issues
159
<!-- Mention the issue this PR relates to. E.g.: Fixes #1 -->
10+
Fixes #

.travis.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ stages:
1111
install:
1212
- pip install -r requirements-dev.txt
1313

14-
script: pylint swarmlib
14+
script:
15+
- pylint swarmlib tests
16+
- pytest tests
1517

1618
jobs:
1719
include:

.vscode/settings.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"pylint",
2525
"pypi",
2626
"pyplot",
27+
"pytest",
2728
"qsize",
2829
"swarmlib",
2930
"wfunc"
@@ -33,11 +34,15 @@
3334
"files.trimFinalNewlines": true,
3435
"python.linting.enabled": true,
3536
"python.linting.pylintEnabled": true,
37+
"python.testing.pytestEnabled": true,
3638
"python.pythonPath": ".venv/bin/python",
3739
"colorInfo.languages": [
3840
{
3941
"selector": "python",
4042
"colors": "hex"
4143
}
42-
]
44+
],
45+
"files.associations": {
46+
".pylintrc": "ini"
47+
}
4348
}

pytest.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
addopts = --spec --cov swarmlib

requirements-dev.txt

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
-r requirements.txt
22

3+
pytest
4+
pytest-describe
5+
pytest-spec
6+
pytest-cov
7+
autopep8
38
pylint
49
pylint-quotes
510
pylintfileheader

tests/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# ------------------------------------------------------------------------------------------------------
2+
# Copyright (c) Leo Hanisch. All rights reserved.
3+
# Licensed under the BSD 3-Clause License. See LICENSE.txt in the project root for license information.
4+
# ------------------------------------------------------------------------------------------------------

tests/util/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# ------------------------------------------------------------------------------------------------------
2+
# Copyright (c) Leo Hanisch. All rights reserved.
3+
# Licensed under the BSD 3-Clause License. See LICENSE.txt in the project root for license information.
4+
# ------------------------------------------------------------------------------------------------------

tests/util/coordinate_test.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# ------------------------------------------------------------------------------------------------------
2+
# Copyright (c) Leo Hanisch. All rights reserved.
3+
# Licensed under the BSD 3-Clause License. See LICENSE.txt in the project root for license information.
4+
# ------------------------------------------------------------------------------------------------------
5+
6+
import numpy as np
7+
8+
9+
import pytest
10+
11+
from swarmlib.util.coordinate import Coordinate
12+
13+
# pylint: disable=unused-variable
14+
15+
16+
@pytest.fixture
17+
def test_func():
18+
return lambda x: np.sum(x) # pylint: disable=unnecessary-lambda
19+
20+
21+
@pytest.fixture
22+
def test_object(test_func):
23+
return Coordinate(
24+
function=test_func,
25+
bit_generator=np.random.default_rng(3),
26+
lower_boundary=0.1,
27+
upper_boundary=3.9)
28+
29+
30+
def describe_coordinate():
31+
def describe_constructor():
32+
def describe_raise_error():
33+
def if_bit_generator_missing():
34+
with pytest.raises(KeyError):
35+
Coordinate(function='foo')
36+
37+
def if_function_is_missing():
38+
with pytest.raises(KeyError):
39+
Coordinate(bit_generator='foo')
40+
41+
def initializes_correctly(test_object):
42+
np.testing.assert_array_equal(test_object.position, [0.42546683514577255, 0.9998799250651788])
43+
np.testing.assert_equal(test_object.value, 1.4253467602109513)
44+
np.testing.assert_array_less(test_object.position, 3.9)
45+
np.testing.assert_array_less(0.1, test_object.position)
46+
47+
def describe_comparison():
48+
@pytest.fixture
49+
def other(test_func):
50+
return Coordinate(
51+
function=test_func,
52+
bit_generator=np.random.default_rng(4),
53+
lower_boundary=0.1,
54+
upper_boundary=3.9)
55+
56+
def equal(test_object):
57+
assert test_object == test_object # pylint: disable=comparison-with-itself
58+
59+
def not_equal(test_object, other):
60+
assert test_object != other
61+
62+
def less(test_object, other):
63+
assert test_object < other
64+
65+
def less_equal(test_object, other):
66+
assert test_object <= other
67+
assert test_object <= test_object # pylint: disable=comparison-with-itself
68+
69+
def greater(test_object, other):
70+
assert other > test_object
71+
72+
def greater_equal(test_object, other):
73+
assert other >= test_object
74+
assert test_object >= test_object # pylint: disable=comparison-with-itself
75+
76+
def describe_position():
77+
def setter_clips_position_and_updates_value(test_object):
78+
test_object._position = [-5, 7] # pylint: disable=protected-access
79+
80+
np.testing.assert_array_equal(test_object.position, [0.1, 3.9])
81+
np.testing.assert_equal(test_object.value, 4)

0 commit comments

Comments
 (0)