In [1]:
def kepler_loc(p1, p2, dt, t):
    ...
    return p3

In [2]:
def test_kepler_loc():
    p1 = jupiter(two_days_ago)
    p2 = jupiter(yesterday)
    exp = jupiter(today)
    obs = kepler_loc(p1, p2, 1, 1)
    if exp != obs:
        raise ValueError("Jupiter is not where it should be!")

In [3]:
def test_func():
    exp = get_expected()
    obs = func(*args, **kwargs)
    assert exp == obs

In [4]:
def test_keppler_loc():
    p1 = jupiter(two_days_ago)
    p2 = jupiter(yesterday)
    exp = jupiter(today)
    obs = keppler_loc(p1, p2, 1, 1)
    assert exp == obs

In [5]:
from nose.tools import assert_equal

def test_kepler_loc():
    p1 = jupiter(two_days_ago)
    p2 = jupiter(yesterday)
    exp = jupiter(today)
    obs = keppler_loc(p1, p2, 1, 1)
    assert_equal(exp, obs)

In [6]:
def fib(n):
    if n == 0 or n == 1:
        return 1
    else:
        return fib(n - 1) + fib(n - 2)

In [7]:
from nose.tools import assert_equal

def test_fib0():
    # test edge 0
    obs = fib(0)
    assert_equal(1, obs)

def test_fib1():
    # test edge 1
    obs = fib(1)
    assert_equal(1, obs)

def test_fib6():
    # test regular point
    obs = fib(6)
    assert_equal(13, obs)

In [8]:
# Running the test functions manually should produce no output if the tests pass
test_fib0()
test_fib1()
test_fib6()

In [9]:
import numpy as np

def sinc2d(x, y):
    if x == 0.0 and y == 0.0:
        return 1.0
    elif x == 0.0:
        return np.sin(y) / y
    elif y == 0.0:
        return np.sin(x) / x
    else:
        return (np.sin(x) / x) * (np.sin(y) / y)

In [10]:
import numpy as np
from nose.tools import assert_equal

def test_internal():
    exp = (2.0 / np.pi) * (-2.0 / (3.0 * np.pi))
    obs = sinc2d(np.pi / 2.0, 3.0 * np.pi / 2.0)
    assert_equal(exp, obs)

def test_edge_x():
    exp = (-2.0 / (3.0 * np.pi))
    obs = sinc2d(0.0, 3.0 * np.pi / 2.0)
    assert_equal(exp, obs)

def test_edge_y():
    exp = (2.0 / np.pi)
    obs = sinc2d(np.pi / 2.0, 0.0)
    assert_equal(exp, obs)

def test_corner():
    exp = 1.0
    obs = sinc2d(0.0, 0.0)
    assert_equal(exp, obs)

In [11]:
# run the tests for sinc2() manually here!

In [12]:
def a(x):
    return x + 1

def b(x):
    return 2 * x

def c(x):
    return b(a(x))

In [13]:
from nose.tools import assert_equal

def test_c():
    exp = 6
    obs = c(2)
    assert_equal(exp, obs)
    
test_c()

In [14]:
from nose.tools import assert_equal

def test_std1():
    obs = std([0.0, 2.0])
    exp = 1.0
    assert_equal(obs, exp)

In [15]:
def std(vals):
    # surely this is cheating...
    return 1.0

# run the test
test_std1()

In [16]:
def test_std1():
    obs = std([0.0, 2.0])
    exp = 1.0
    assert_equal(obs, exp)

def test_std2():
    obs = std([]) 
    exp = 0.0
    assert_equal(obs, exp)

def test_std3():
    obs = std([0.0, 4.0])
    exp = 2.0
    assert_equal(obs, exp)
    
# run the tests
test_std1()
test_std2()
test_std3()


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-16-7d31efb13ae3> in <module>()
     16 # run the tests
     17 test_std1()
---> 18 test_std2()
     19 test_std3()

<ipython-input-16-7d31efb13ae3> in test_std2()
      7     obs = std([])
      8     exp = 0.0
----> 9     assert_equal(obs, exp)
     10 
     11 def test_std3():

/home/scopatz/miniconda3/lib/python3.4/unittest/case.py in assertEqual(self, first, second, msg)
    795         """
    796         assertion_func = self._getAssertEqualityFunc(first, second)
--> 797         assertion_func(first, second, msg=msg)
    798 
    799     def assertNotEqual(self, first, second, msg=None):

/home/scopatz/miniconda3/lib/python3.4/unittest/case.py in _baseAssertEqual(self, first, second, msg)
    788             standardMsg = '%s != %s' % _common_shorten_repr(first, second)
    789             msg = self._formatMessage(msg, standardMsg)
--> 790             raise self.failureException(msg)
    791 
    792     def assertEqual(self, first, second, msg=None):

AssertionError: 1.0 != 0.0

In [17]:
def std(vals):
    # a little better
    if len(vals) == 0:
        return 0.0
    return vals[-1] / 2.0

In [18]:
def test_std1():
    obs = std([0.0, 2.0])
    exp = 1.0
    assert_equal(obs, exp)

def test_std2():
    obs = std([])
    exp = 0.0
    assert_equal(obs, exp)

def test_std3():
    obs = std([0.0, 4.0])
    exp = 2.0
    assert_equal(obs, exp)

def test_std4():
    obs = std([1.0, 3.0])
    exp = 1.0
    assert_equal(obs, exp)

def test_std5():
    obs = std([1.0, 1.0, 1.0])
    exp = 0.0
    assert_equal(obs, exp)

# run the tests
test_std1()
test_std2()
test_std3()
test_std4()
test_std5()


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-18-e0b9b2ac3617> in <module>()
     28 test_std2()
     29 test_std3()
---> 30 test_std4()
     31 test_std5()

<ipython-input-18-e0b9b2ac3617> in test_std4()
     17     obs = std([1.0, 3.0])
     18     exp = 1.0
---> 19     assert_equal(obs, exp)
     20 
     21 def test_std5():

/home/scopatz/miniconda3/lib/python3.4/unittest/case.py in assertEqual(self, first, second, msg)
    795         """
    796         assertion_func = self._getAssertEqualityFunc(first, second)
--> 797         assertion_func(first, second, msg=msg)
    798 
    799     def assertNotEqual(self, first, second, msg=None):

/home/scopatz/miniconda3/lib/python3.4/unittest/case.py in _baseAssertEqual(self, first, second, msg)
    788             standardMsg = '%s != %s' % _common_shorten_repr(first, second)
    789             msg = self._formatMessage(msg, standardMsg)
--> 790             raise self.failureException(msg)
    791 
    792     def assertEqual(self, first, second, msg=None):

AssertionError: 1.5 != 1.0

In [19]:
def std(vals):
    # finally, some math
    n = len(vals)
    if n == 0:
        return 0.0
    mu = sum(vals) / n
    var = 0.0
    for val in vals:
        var = var + (val - mu)**2
    return (var / n)**0.5

In [20]:
# run the tests
test_std1()
test_std2()
test_std3()
test_std4()
test_std5()