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

move tests to tests folder #211

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
11 changes: 2 additions & 9 deletions INSTALL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ or, if additional access rights are needed (Unix):

sudo python setup.py install

* The tests programs (test_*.py) are meant to be run through the Nose
testing framework. This can be achieved for instance with a command
* The tests programs (test_*.py) are meant to be run through pytest. This can be achieved for instance with a command
like

nosetests -sv uncertainties/

or simply

nosetests uncertainties/

(for a less verbose output).
pytest ./tests
10 changes: 1 addition & 9 deletions uncertainties/unumpy/test_ulinalg.py → tests/test_ulinalg.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
"""
Tests for uncertainties.unumpy.ulinalg.

These tests can be run through the Nose testing framework.

(c) 2010-2016 by Eric O. LEBIGOT (EOL) <eric.lebigot@normalesup.org>.
"""

# Some tests are already performed in test_unumpy (unumpy contains a
# matrix inversion, for instance). They are not repeated here.

Expand All @@ -18,7 +10,7 @@
sys.exit() # There is no reason to test the interface to NumPy

from uncertainties import unumpy, ufloat
from uncertainties.unumpy.test_unumpy import arrays_close
from uncertainties.testing import arrays_close

def test_list_inverse():
"Test of the inversion of a square matrix"
Expand Down
43 changes: 15 additions & 28 deletions uncertainties/test_umath.py → tests/test_umath.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
"""
Tests of the code in uncertainties.umath.

These tests can be run through the Nose testing framework.

(c) 2010-2016 by Eric O. LEBIGOT (EOL).
"""

from __future__ import division
from __future__ import absolute_import

# Standard modules
import sys
import math
from math import isnan, isinf

# Local modules:
from uncertainties import ufloat
import uncertainties.core as uncert_core
import uncertainties.umath_core as umath_core

from . import test_uncertainties

from uncertainties.testing import compare_derivatives, numbers_close
from test_uncertainties import power_special_cases, power_all_cases, power_wrt_ref
###############################################################################
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not seem right to import from one test_ file to another. These functions do seem too specialized for uncertainties.testing though. Could we use one of the options in this StackOverflow question. Maybe the conftest.py and fixture option, or the pythonpath and helpers file option?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding testpaths = ["tests"] is not required for tests to work right? It's just a convenience that makes pytest discover tests more quickly? Is my understanding correct?

In the past I've put helper function needed for testing in an __init__.py file in the tests folder. These objects can then be imported within the test scripts. We could also have a tests/helper.py module to accomplish the same result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, importing across test files does feel a bit odd. The helpers.py solution is nice and simple.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding testpaths = ["tests"] is not required for tests to work right? It's just a convenience that makes pytest discover tests more quickly? Is my understanding correct?

Yes, that seems to be all it does functionally. My conjecture was that it might also help IDE's figure out that the project uses pytest as the test runner because otherwise there is no configuration indicating that pytest should be run, but I haven't tried to confirm that.

I like the move to helpers.py. We might want to add

testpaths = ["tests"]
addopts = [
    "--import-mode=importlib",
]

to pyproject.toml like the one StackOverflow answer suggests, but we could wait for that. Currently, pytest adds all the test directories to the Python path so you can do import helpers but it has been trying to move away from that to just importing the test files with importlib without modifying the Python path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for explaining, wouldn't have understood why otherwise

# Unit tests

Expand All @@ -31,13 +19,12 @@ def test_fixed_derivatives_math_funcs():
"""

for name in umath_core.many_scalars_to_scalar_funcs:
# print "Checking %s..." % name
func = getattr(umath_core, name)
# Numerical derivatives of func: the nominal value of func() results
# is used as the underlying function:
numerical_derivatives = uncert_core.NumericalDerivatives(
lambda *args: func(*args))
test_uncertainties.compare_derivatives(func, numerical_derivatives)
compare_derivatives(func, numerical_derivatives)

# Functions that are not in umath_core.many_scalars_to_scalar_funcs:

Expand All @@ -48,11 +35,11 @@ def frac_part_modf(x):
def int_part_modf(x):
return umath_core.modf(x)[1]

test_uncertainties.compare_derivatives(
compare_derivatives(
frac_part_modf,
uncert_core.NumericalDerivatives(
lambda x: frac_part_modf(x)))
test_uncertainties.compare_derivatives(
compare_derivatives(
int_part_modf,
uncert_core.NumericalDerivatives(
lambda x: int_part_modf(x)))
Expand All @@ -64,11 +51,11 @@ def mantissa_frexp(x):
def exponent_frexp(x):
return umath_core.frexp(x)[1]

test_uncertainties.compare_derivatives(
compare_derivatives(
mantissa_frexp,
uncert_core.NumericalDerivatives(
lambda x: mantissa_frexp(x)))
test_uncertainties.compare_derivatives(
compare_derivatives(
exponent_frexp,
uncert_core.NumericalDerivatives(
lambda x: exponent_frexp(x)))
Expand Down Expand Up @@ -172,7 +159,7 @@ def monte_carlo_calc(n_samples):
# or assert_array_max_ulp. This is relevant for all vectorized
# occurrences of numbers_close.

assert numpy.vectorize(test_uncertainties.numbers_close)(
assert numpy.vectorize(numbers_close)(
covariances_this_module,
covariances_samples,
0.06).all(), (
Expand All @@ -183,7 +170,7 @@ def monte_carlo_calc(n_samples):
)

# The nominal values must be close:
assert test_uncertainties.numbers_close(
assert numbers_close(
nominal_value_this_module,
nominal_value_samples,
# The scale of the comparison depends on the standard
Expand Down Expand Up @@ -279,14 +266,14 @@ def test_hypot():
# Derivatives that cannot be calculated simply return NaN, with no
# exception being raised, normally:
result = umath_core.hypot(x, y)
assert test_uncertainties.isnan(result.derivatives[x])
assert test_uncertainties.isnan(result.derivatives[y])
assert isnan(result.derivatives[x])
assert isnan(result.derivatives[y])

def test_power_all_cases():
'''
Test special cases of umath_core.pow().
'''
test_uncertainties.power_all_cases(umath_core.pow)
power_all_cases(umath_core.pow)

# test_power_special_cases() is similar to
# test_uncertainties.py:test_power_special_cases(), but with small
Expand All @@ -297,7 +284,7 @@ def test_power_special_cases():
Checks special cases of umath_core.pow().
'''

test_uncertainties.power_special_cases(umath_core.pow)
power_special_cases(umath_core.pow)

# We want the same behavior for numbers with uncertainties and for
# math.pow() at their nominal values.
Expand Down Expand Up @@ -341,4 +328,4 @@ def test_power_wrt_ref():
'''
Checks special cases of the umath_core.pow() power operator.
'''
test_uncertainties.power_wrt_ref(umath_core.pow, math.pow)
power_wrt_ref(umath_core.pow, math.pow)
Loading