Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c6918cf

Browse files
lucasgcbOrtnerMichael
authored andcommittedMar 12, 2019
Add Testing Harness base (magpylib#56)
* Add tox pytest configuration * Add test examples * Add checkDimension utility test case, edge case fix * Comment out unused test error messages * Add circleci configuration * Add test summary report to CircleCI * Fix test running pytest instead of tox interface * Tweak path for storing test results in CircleCI * Add test reports xml to tox output * Change directory for storing tests on tox * Store test artifacts in CircleCI * Fix identation on circleci yml for test summary * Store artifacts before storing test results * Testing circleci output * Check test results in Circle CI for summary * Explicit test result file in Circle CI * Upload explicit result file to circleci * Add subdirectory to test result xml * Replace XML coverage with Test Summary in tox * Add Circle CI badge to Readme * Remove placeholder configuration from CircleCI * Add Box test cases - Error for zero magnetization vector - Error for zero dim vector - Edge Case Error * Add test for fastnorm3D change docstring for test_randomAxis
1 parent a2b5fca commit c6918cf

File tree

10 files changed

+114
-1
lines changed

10 files changed

+114
-1
lines changed
 

‎.circleci/config.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: 2
2+
jobs:
3+
build:
4+
docker:
5+
- image: python:3
6+
steps:
7+
- checkout
8+
- run:
9+
name: Install Testing Env
10+
command: mkdir test-results && pip install tox
11+
- run:
12+
name: Run Test Suite
13+
command: tox
14+
- store_artifacts:
15+
path: test-results/magpylib/
16+
- store_test_results:
17+
path: test-results/magpylib/

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pip-delete-this-directory.txt
4343

4444
# Unit test / coverage reports
4545
htmlcov/
46+
test-results/
4647
.tox/
4748
.nox/
4849
.coverage

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
[![CircleCI](https://circleci.com/gh/OrtnerMichael/magPyLib.svg?style=svg)](https://circleci.com/gh/OrtnerMichael/magPyLib)
12
[![](https://readthedocs.com/projects/magpylib-magpylib/badge/?version=sphinx-docs)](https://magpylib-magpylib.readthedocs-hosted.com/)
23

34
---
45

56
# About
67
*A simple and user friendly magnetic toolbox for Python 3.2+*
78

8-
99
### What is Magpylib ?
1010
- Magpylib is a Python library for calculating magnetic fields from permanent magnets and current distributions.
1111
- It provides an intuitive magnetic source class to quickly generate, group, geometrically manipulate and visualize assemblies of magnets and currents and the fields they generate.

‎magpylib/_lib/utility.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from numpy import float64, isnan, array
33
## Helper function for validating input dimensions
44
def checkDimensions(expectedD: int, dim: Tuple[float,float,float], exitMsg: str="Bad dim input") -> array:
5+
if type(dim)==int or type(dim)==float:
6+
dim=[dim]
57
assert all(coord == 0 for coord in dim) is False, exitMsg + ", all values are zero"
68
dimension = array(dim, dtype=float64, copy=False)
79
assert (not any(isnan(dimension)) and len(dimension) == expectedD), exitMsg

‎tests/__init__.py

Whitespace-only changes.

‎tests/test_magnets.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from magpylib.source import magnet
2+
import pytest
3+
4+
def test_BoxZeroMagError():
5+
with pytest.raises(AssertionError):
6+
magnet.Box(mag=[0,0,0],dim=[1,1,1])
7+
8+
def test_BoxZeroDimError():
9+
with pytest.raises(AssertionError):
10+
magnet.Box(mag=[1,1,1],dim=[0,0,0])
11+
12+
def test_BoxEdgeCase1():
13+
## For now this returns NaN, may be an analytical edge case
14+
## Test the Methods in getB() before moving onto this
15+
from numpy import isnan
16+
pm = magnet.Box(mag=[0,0,1000],dim=[0.5,0.1,1],pos=[.25,.55,-1111])
17+
result = pm.getB([.5,.5,5])
18+
assert any(isnan(result)), "Results from getB is not NaN"

‎tests/test_privateMath.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import unittest
2+
from magpylib._lib.mathLibPrivate import fastSum3D, fastNorm3D
3+
import numpy
4+
5+
6+
def test_fastSum3D():
7+
"""
8+
#Test if it can sum a position vector
9+
"""
10+
position=[2.3,5.6,2.0]
11+
assert round(fastSum3D(position),2)==9.90, "Error, not adding correctly"
12+
13+
###
14+
15+
def test_fastNorm3D():
16+
from numpy import isnan
17+
result=round(fastNorm3D([58.2,25,25]),4)
18+
assert result==68.0973, "Expected 68.0973, got " + str(result)
19+

‎tests/test_publicMath.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
from magpylib._lib.mathLibPublic import randomAxis, axisFromAngles, anglesFromAxis, rotatePosition
3+
import numpy
4+
5+
def test_randomAxis():
6+
"""
7+
#Test if returned random axis are valid
8+
"""
9+
result = randomAxis()
10+
assert len(result)==3, "Returning axis should be 3"
11+
assert all(type(axis)==numpy.float64 for axis in result), "Axis values are not float64"
12+
assert all(abs(axis)<=1 for axis in result), "Absolute axis values returned greater than 1"

‎tests/test_utility.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from typing import Tuple
2+
from magpylib._lib.utility import checkDimensions
3+
from numpy import float64, isnan, array
4+
import pytest
5+
6+
def test_checkDimensionZero():
7+
# errMsg = "Did not raise all zeros Error"
8+
with pytest.raises(AssertionError):
9+
checkDimensions(3,dim=(.0,0,.0))
10+
11+
with pytest.raises(AssertionError):
12+
checkDimensions(0,dim=[])
13+
14+
def test_checkDimensionMembers():
15+
# errMsg = "Did not raise expected Value Error"
16+
with pytest.raises(ValueError):
17+
checkDimensions(3,dim=(3,'r',6))
18+
19+
def test_checkDimensionSize():
20+
errMsg = "Did not raise wrong dimension size Error"
21+
with pytest.raises(AssertionError) as error:
22+
checkDimensions(3,dim=(3,5,9,6))
23+
assert error.type == AssertionError, errMsg
24+
25+
def test_checkDimensionReturn():
26+
errMsg = "Wrong return dimension size"
27+
result = checkDimensions(4,dim=(3,5,9,10))
28+
assert len(result) == 4, errMsg
29+
result = checkDimensions(3,dim=(3,5,9))
30+
assert len(result) == 3, errMsg
31+
result = checkDimensions(2,dim=(3,5))
32+
assert len(result) == 2, errMsg
33+
result = checkDimensions(1,dim=(3))
34+
assert len(result) == 1, errMsg
35+

‎tox.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[tox]
2+
envlist = py32
3+
4+
[testenv]
5+
# install pytest in the virtualenv where commands will be executed
6+
deps = pytest-cov
7+
commands =
8+
# NOTE: you can run any command line tool here - not just tests
9+
py.test --junitxml=test-results/magpylib/results.xml --cov-report html --cov-report term:skip-covered --cov=magpylib

0 commit comments

Comments
 (0)
Please sign in to comment.