Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chmp committed Feb 7, 2016
0 parents commit af1bb53
Show file tree
Hide file tree
Showing 8 changed files with 722 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/.eggs/
/.cache/

__pycache__/
*.pyc
*.egg-info/
.ipynb_checkpoints/

20 changes: 20 additions & 0 deletions License.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2015 - 2016 Christopher Prohm

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
155 changes: 155 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# ipytest - unit tests in IPython notbeooks

Sometimes quick experiments in IPython grow large and you find yourself wanting
unit tests. This module aims to make testing code in IPython notebooks easy. At
its core, it offers a test runner that execute all tests defined inside the
notebook environment. It is also designed to make the transfer of the tests into
proper python modules easy.

## Example

::python

# coding: utf-8
# In[1]:
from __future__ import print_function, division, absolute_import
import ipytest

# In[2]:
def fibonacci(i):
"""Compute the fibonacci sequence.
>>> [fibonacci(n) for n in range(7)]
[1, 1, 2, 3, 5, 8, 13]
"""
if i == 0 or i == 1:
return 1
return fibonacci(i - 1) + fibonacci(i - 2)

# In[3]:
ipytest.clean_tests("test_fibonacci*")

def test_fibonacci_0():
assert fibonacci(0) == 1

def test_fibonacci_1():
assert fibonacci(1) == 1

def test_fibonacci_2():
assert fibonacci(2) == 2
def test_fibonacci_3():
assert fibonacci(3) == 3
def test_fibonacci_4():
assert fibonacci(4) == 5

def test_fibonacci_5():
assert fibonacci(5) == 8
def test_fibonacci_6():
assert fibonacci(6) == 13
ipytest.run_tests(doctest=True)


## Installation

::bash
pip install ipytest


## Features

- simple interface
- builds on standard unittest
- support for doctests
- support for pandas and numpy.

## Reference

### ipytest.run_tests(doctest=False, items=None)

Run all tests in the given items dictionary.

**Arguments:**

- `doctest`: if True search for doctests.
- `items`: the globals object containing the tests. If `None` is given, the
globals object is determined from the call stack.

### ipytest.clean_tests(pattern="test*", items=None)

Delete tests with names matching the given pattern.

In IPython the results of all evaluations are kept in global variables
unless explicitly deleted. Therefore, previous definitions will still be found when tests are renamed but not explicitly deleted.

An effecitve pattern is to start with the cell containing tests with a call
to `clean_tests`, then defined all test cases, and finally call `run_tests`.
This way renaming tests works as expected.

**Arguments:**

- `pattern`: a glob pattern used to match the tests to delete.
- `items`: the globals object containing the tests. If `None` is given, the
globals object is determined from the call stack.

### ipytest.collect_tests(doctest=False, items=None)

Collect all test cases and return a `unittest.TestSuite`.

The arguments are the same as for `ipytest.run_tests`.

### ipytest.assert_equals(a, b, *args, **kwargs)

Compare two objects and throw and exception if they are not equal.

This method uses `ipytest.get_assert_function` to determine the assert
implementation to use depending on the argument type.

**Arguments**

- `a`, `b`: the two objects to compare.
- `args`, `kwargs`: (keyword) arguments that are passed to the underlying
test function. This option can, for example, be used to set the
tolerance when comparing `numpy.array` objects

### ipytest.get_assert_function(a, b)

Determine the assert function to use depending on the arguments.

If either object is a `numpy .ndarray`, a `pandas.Series`, a
`pandas.DataFrame`, or `pandas.Panel`, it returns the assert functions
supplied by `numpy` and `pandas`. Otherwise, it defaults to
`ipytest.unittest_assert_equals`

### ipytest.unittest_assert_equals(a, b)

Compare two objects with the `assertEqual` method of `unittest.TestCase`.


## License

The MIT License (MIT)
Copyright (c) 2015 - 2016 Christopher Prohm

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

130 changes: 130 additions & 0 deletions example/Example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from __future__ import print_function, division, absolute_import"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import ipytest"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def fibonacci(i):\n",
" \"\"\"Compute the fibonacci sequence.\n",
" \n",
" >>> [fibonacci(n) for n in range(7)]\n",
" [1, 1, 2, 3, 5, 8, 13]\n",
" \n",
" \"\"\"\n",
" if i == 0 or i == 1:\n",
" return 1\n",
" return fibonacci(i - 1) + fibonacci(i - 2)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"unittest.case.FunctionTestCase (test_fibonacci_0) ... ok\n",
"unittest.case.FunctionTestCase (test_fibonacci_1) ... ok\n",
"unittest.case.FunctionTestCase (test_fibonacci_2) ... ok\n",
"unittest.case.FunctionTestCase (test_fibonacci_3) ... ok\n",
"unittest.case.FunctionTestCase (test_fibonacci_4) ... ok\n",
"unittest.case.FunctionTestCase (test_fibonacci_5) ... ok\n",
"unittest.case.FunctionTestCase (test_fibonacci_6) ... ok\n",
"runTest (ipytest._DocTestCase) ... ok\n",
"\n",
"----------------------------------------------------------------------\n",
"Ran 8 tests in 0.085s\n",
"\n",
"OK\n"
]
}
],
"source": [
"ipytest.clean_tests(\"test_fibonacci*\")\n",
"\n",
"def test_fibonacci_0():\n",
" assert fibonacci(0) == 1\n",
"\n",
"def test_fibonacci_1():\n",
" assert fibonacci(1) == 1\n",
"\n",
"def test_fibonacci_2():\n",
" assert fibonacci(2) == 2\n",
" \n",
"def test_fibonacci_3():\n",
" assert fibonacci(3) == 3\n",
" \n",
"def test_fibonacci_4():\n",
" assert fibonacci(4) == 5\n",
"\n",
"def test_fibonacci_5():\n",
" assert fibonacci(5) == 8\n",
" \n",
"def test_fibonacci_6():\n",
" assert fibonacci(6) == 13\n",
" \n",
"ipytest.run_tests(doctest=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading

0 comments on commit af1bb53

Please sign in to comment.