Skip to content

Instantly share code, notes, and snippets.

@elventear
Created July 14, 2016 21:30
Show Gist options
  • Save elventear/14c0db5d08cc5952c15cfdd8a4e40637 to your computer and use it in GitHub Desktop.
Save elventear/14c0db5d08cc5952c15cfdd8a4e40637 to your computer and use it in GitHub Desktop.
Numerical Testing Sample with py.test parametrization

Original

Output

[2.7.10;@radtrack tests]$ py.test -v AnalyticCalc1_test.py
========================================== test session starts ===========================================
platform linux2 -- Python 2.7.10, pytest-2.8.3, py-1.4.30, pluggy-0.3.1 -- /home/vagrant/.pyenv/versions/2.7.10/bin/python
cachedir: ../.cache
rootdir: /home/vagrant/radtrack, inifile:
plugins: xdist-1.13.1
collected 1 items

AnalyticCalc1_test.py::test_1 FAILED

================================================ FAILURES ================================================
_________________________________________________ test_1 _________________________________________________

    def test_1():
        _run('IDWaveLengthPhotonEnergy.yml')
        _run('CriticalEnergyWiggler.yml')
        _run('RadiatedPowerPlanarWiggler.yml')
        _run('CentralPowerDensityPlanarWiggler.yml')
>       _run('UndulatorSourceSizeDivergence.yml')

AnalyticCalc1_test.py:63:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
AnalyticCalc1_test.py:34: in _run
    _assert_array(case['Expect'], actual)
AnalyticCalc1_test.py:48: in _assert_array
    _assert(e, a)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

expect = 8.7718048723e-06, actual = 8.7734070190701501e-05, expected_error = 0.01

    def _assert(expect, actual, expected_error=0.01):
        if _EPSILON > abs(expect):
            assert _EPSILON > abs(actual)
            return
        elif _EPSILON > abs(actual):
            raise AssertionError(
                'expect {} != {} actual'.format(expect, actual))
>       assert expected_error > abs(expect/actual - 1)
E       assert 0.01 > 0.90001826139795715
E        +  where 0.90001826139795715 = abs(((8.7718048723e-06 / 8.7734070190701501e-05) - 1))

AnalyticCalc1_test.py:43: AssertionError
------------------------------------------ Captured stdout call ------------------------------------------
[u'***', u'IDWaveLengthPhotonEnergy.yml']
(2.181357, 0.0, 1.3235164823332067e-09, 937.427086523916)
[2.181, 0.0, 1.324e-09, 937.427]
[u'***', u'CriticalEnergyWiggler.yml']
4478.42575113
4478.0
[u'***', u'RadiatedPowerPlanarWiggler.yml']
(9778.256526338504, 4.59)
[9778.0, 4.59]
[u'***', u'CentralPowerDensityPlanarWiggler.yml']
7179.92051438
7180.0
[u'***', u'UndulatorSourceSizeDivergence.yml']
(8.7734070190701501e-05, 1.2009437102574673e-06)
[8.7718048723e-06, 1.20072440115e-05]
======================================== 1 failed in 0.24 seconds ========================================

Single test function per test, with parameters

Test code

@pytest.mark.parametrize("lam_rn,L_id,expected", [(1.324e-09, 459, [8.7718048723e-06, 1.20072440115e-05])])
def test_undulator_source_divergence(lam_rn, L_id, expected):
    actual = AnalyticCalc.UndulatorSourceSizeDivergence(lam_rn, L_id)
    _assert_array(expected, actual)

Output

[2.7.10;@radtrack tests]$ py.test -v AnalyticCalc1_test.py
=========================================== test session starts ============================================
platform linux2 -- Python 2.7.10, pytest-2.8.3, py-1.4.30, pluggy-0.3.1 -- /home/vagrant/.pyenv/versions/2.7.10/bin/python
cachedir: ../.cache
rootdir: /home/vagrant/radtrack, inifile:
plugins: xdist-1.13.1
collected 2 items

AnalyticCalc1_test.py::test_1 PASSED
AnalyticCalc1_test.py::test_undulator_source_divergence[1.324e-09-459-expected0] FAILED

================================================= FAILURES =================================================
________________________ test_undulator_source_divergence[1.324e-09-459-expected0] _________________________

lam_rn = 1.324e-09, L_id = 459, expected = [8.7718048723e-06, 1.20072440115e-05]

    @pytest.mark.parametrize("lam_rn,L_id,expected", [(1.324e-09, 459, [8.7718048723e-06, 1.20072440115e-05])])
    def test_undulator_source_divergence(lam_rn, L_id, expected):
        actual = AnalyticCalc.UndulatorSourceSizeDivergence(lam_rn, L_id)
>       _assert_array(expected, actual)

AnalyticCalc1_test.py:72:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
AnalyticCalc1_test.py:48: in _assert_array
    _assert(e, a)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

expect = 8.7718048723e-06, actual = 8.7734070190701501e-05, expected_error = 0.01

    def _assert(expect, actual, expected_error=0.01):
        if _EPSILON > abs(expect):
            assert _EPSILON > abs(actual)
            return
        elif _EPSILON > abs(actual):
            raise AssertionError(
                'expect {} != {} actual'.format(expect, actual))
>       assert expected_error > abs(expect/actual - 1)
E       assert 0.01 > 0.90001826139795715
E        +  where 0.90001826139795715 = abs(((8.7718048723e-06 / 8.7734070190701501e-05) - 1))

AnalyticCalc1_test.py:43: AssertionError
==================================== 1 failed, 1 passed in 0.26 seconds ====================================

Test runner function with py.test parametrization

Test code

@pytest.mark.parametrize('fn,kwargs,expect', [
    ('UndulatorSourceSizeDivergence', {'lam_rn': 1.324e-09, 'L_id': 459}, [8.7718048723e-06, 1.20072440115e-05])
], ids=['UndulatorSourceSizeDivergence'])
def test_runner(fn, kwargs, expect):
    f = getattr(AnalyticCalc, fn)
    actual = f(**kwargs)
    _assert_array(expect, actual)

Output

[2.7.10;@radtrack tests]$ py.test -v AnalyticCalc1_test.py
====================================== test session starts =======================================
platform linux2 -- Python 2.7.10, pytest-2.8.3, py-1.4.30, pluggy-0.3.1 -- /home/vagrant/.pyenv/versions/2.7.10/bin/python
cachedir: ../.cache
rootdir: /home/vagrant/radtrack, inifile:
plugins: xdist-1.13.1
collected 2 items

AnalyticCalc1_test.py::test_1 PASSED
AnalyticCalc1_test.py::test_runner[UndulatorSourceSizeDivergence] FAILED

============================================ FAILURES ============================================
___________________________ test_runner[UndulatorSourceSizeDivergence] ___________________________

fn = 'UndulatorSourceSizeDivergence', kwargs = {'L_id': 459, 'lam_rn': 1.324e-09}
expect = [8.7718048723e-06, 1.20072440115e-05]

    @pytest.mark.parametrize('fn,kwargs,expect', [
        ('UndulatorSourceSizeDivergence', {'lam_rn': 1.324e-09, 'L_id': 459}, [8.7718048723e-06, 1.20072440115e-05])
    ], ids=['UndulatorSourceSizeDivergence'])
    def test_runner(fn, kwargs, expect):
        f = getattr(AnalyticCalc, fn)
        actual = f(**kwargs)
>       _assert_array(expect, actual)

AnalyticCalc1_test.py:76:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
AnalyticCalc1_test.py:48: in _assert_array
    _assert(e, a)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

expect = 8.7718048723e-06, actual = 8.7734070190701501e-05, expected_error = 0.01

    def _assert(expect, actual, expected_error=0.01):
        if _EPSILON > abs(expect):
            assert _EPSILON > abs(actual)
            return
        elif _EPSILON > abs(actual):
            raise AssertionError(
                'expect {} != {} actual'.format(expect, actual))
>       assert expected_error > abs(expect/actual - 1)
E       assert 0.01 > 0.90001826139795715
E        +  where 0.90001826139795715 = abs(((8.7718048723e-06 / 8.7734070190701501e-05) - 1))

AnalyticCalc1_test.py:43: AssertionError
=============================== 1 failed, 1 passed in 0.26 seconds ===============================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment