ipytest#
Setup#
[1]:
# Set the file name (required)
__file__ = 'testing.ipynb'
# Add ipython magics
# Add ipython magics
import ipytest
import pytest
ipytest.autoconfig()
Test Case#
[2]:
%%ipytest
def test_sorted():
assert sorted([4, 2, 1, 3]) == [1, 2, 3, 4]
. [100%]
1 passed in 0.01s
Test Fixture#
[3]:
%%ipytest
@pytest.fixture
def dict_list():
return [
dict(a='a', b=3),
dict(a='c', b=1),
dict(a='b', b=2),
]
def test_sorted__key_example_1(dict_list):
assert sorted(dict_list, key=lambda d: d['a']) == [
dict(a='a', b=3),
dict(a='b', b=2),
dict(a='c', b=1),
]
def test_sorted__key_example_2(dict_list):
assert sorted(dict_list, key=lambda d: d['b']) == [
dict(a='c', b=1),
dict(a='b', b=2),
dict(a='a', b=3),
]
.. [100%]
2 passed in 0.02s
Test parameterisation#
[4]:
%%ipytest
@pytest.mark.parametrize('input,expected', [
([2, 1], [1, 2]),
('zasdqw', list('adqswz')),
])
def test_examples(input, expected):
actual = sorted(input)
assert actual == expected
.. [100%]
2 passed in 0.02s
Reference#
%%run_pytest …
#
IPython magic that executes first the cell and then run_pytest
. Arguments passed in the cell are passed directly to pytest. The Magics should have been imported with import ipytest.magics
beforehand.
ipytest.run_pytest(module=None, filename=None, pytest_options=(), pytest_plugins=())
#
runs the tests in the existing module (by default main
) with pytest.
Arguments:
module
: the module that contains the tests. If not specified,__main__
is used.filename
: Filename of the file containing the tests. If nothing is specified, the__file__
attribute of the passed module is used.pytest_options
: additional options passed to pytest.pytest_plugins
: additional pytest plugins.
ipytest.run_tests(doctest=False, items=None)
#
Arguments:
doctest
: IfTrue
is specified, angegeben wird, then doctests are searched for.items
: The globals object that contains the tests. IfNone
is specified, the globals object is obtained from the call stack.
ipytest.clean_tests(pattern="test*", items=None)
#
deletes those tests whose names match the specified pattern.
In IPython, the results of all evaluations are saved in global variables, unless they are explicitly deleted. This behavior implies that if tests are renamed, the previous definitions will still be found if they are not deleted. This method aims to simplify this process.
An effective method is clean_tests
to start with a cell, then define all test cases and finally run_tests
call them. That way, renaming tests works as expected.
Arguments:
pattern
: A glob pattern that is used to find the tests to delete.items
: The globals object that contains the tests. IfNone
is specified, the globals object is obtained from the call stack.
ipytest.collect_tests(doctest=False, items=None)
#
collects all test cases and sends them to unittest.TestSuite
.
The arguments are the same as for ipytest.run_tests
.
ipytest.assert_equals(a, b, *args, **kwargs)
#
compares two objects and throws an exception if they are not the same.
The method ipytest.get_assert_function
determines the assert implementation to be used depending on the following arguments:
a, b
: the two objects to be compared.args, kwargs
: (Keyword) arguments that are passed to the underlying test function.
ipytest.get_assert_function(a, b)
#
determines the assert function to be used depending on the arguments.
If one of the objects is numpy.ndarray
, pandas.Series
, pandas.DataFrame
or pandas.Panel
the assert functions provided by numpy
and pandas
will bei returned.
ipytest.unittest_assert_equals(a, b)
#
compares two objects using the assertEqual
method of unittest.TestCase
.