2015-01-26 22:00:11 +08:00
{
2015-05-14 18:55:50 +08:00
" cells " : [
2015-06-19 09:07:36 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-11-01 19:44:00 +08:00
" This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/data-science-ipython-notebooks). "
2015-06-19 09:07:36 +08:00
]
} ,
2015-01-26 22:00:11 +08:00
{
2015-05-14 18:55:50 +08:00
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" # Nose Unit Tests with IPython Notebook "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Nose \n " ,
" \n " ,
" Testing is a vital part of software development. Nose extends unittest to make testing easier. "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Install Nose \n " ,
" \n " ,
" Run the following command line: "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : null ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [ ] ,
" source " : [
" !pip install nose "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Create the Code \n " ,
" \n " ,
" Save your code to a file with the %% file magic: "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 1 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
2015-01-26 22:00:11 +08:00
{
2015-05-14 18:55:50 +08:00
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" Overwriting type_util.py \n "
2015-01-26 22:00:11 +08:00
]
2015-05-14 18:55:50 +08:00
}
] ,
" source " : [
" %% file type_util.py \n " ,
" class TypeUtil: \n " ,
" \n " ,
" @classmethod \n " ,
" def is_iterable(cls, obj): \n " ,
" \" \" \" Determines if obj is iterable. \n " ,
" \n " ,
" Useful when writing functions that can accept multiple types of \n " ,
" input (list, tuple, ndarray, iterator). Pairs well with \n " ,
" convert_to_list. \n " ,
" \" \" \" \n " ,
" try: \n " ,
" iter(obj) \n " ,
" return True \n " ,
" except TypeError: \n " ,
" return False \n " ,
" \n " ,
" @classmethod \n " ,
" def convert_to_list(cls, obj): \n " ,
" \" \" \" Converts obj to a list if it is not a list and it is iterable, \n " ,
" else returns the original obj. \n " ,
" \" \" \" \n " ,
" if not isinstance(obj, list) and cls.is_iterable(obj): \n " ,
" obj = list(obj) \n " ,
" return obj \n "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Create the Nose Tests \n " ,
" \n " ,
" Save your test to a file with the %% file magic: "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 2 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
2015-01-26 22:00:11 +08:00
{
2015-05-14 18:55:50 +08:00
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" Overwriting tests/test_type_util.py \n "
2015-01-26 22:00:11 +08:00
]
2015-05-14 18:55:50 +08:00
}
] ,
" source " : [
" %% file tests/test_type_util.py \n " ,
" from nose.tools import assert_equal \n " ,
" from ..type_util import TypeUtil \n " ,
" \n " ,
" \n " ,
" class TestUtil(): \n " ,
" \n " ,
" def test_is_iterable(self): \n " ,
" assert_equal(TypeUtil.is_iterable( ' foo ' ), True) \n " ,
" assert_equal(TypeUtil.is_iterable(7), False) \n " ,
" \n " ,
" def test_convert_to_list(self): \n " ,
" assert_equal(isinstance(TypeUtil.convert_to_list( ' foo ' ), list), True) \n " ,
" assert_equal(isinstance(TypeUtil.convert_to_list(7), list), False) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Run the Nose Tests \n " ,
" \n " ,
" Run the following command line: "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 3 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
2015-01-26 22:00:11 +08:00
{
2015-05-14 18:55:50 +08:00
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" core.tests.test_type_util.TestUtil.test_convert_to_list ... ok \r \n " ,
" core.tests.test_type_util.TestUtil.test_is_iterable ... ok \r \n " ,
" \r \n " ,
" ---------------------------------------------------------------------- \r \n " ,
" Ran 2 tests in 0.001s \r \n " ,
" \r \n " ,
" OK \r \n "
2015-01-26 22:00:11 +08:00
]
}
] ,
2015-05-14 18:55:50 +08:00
" source " : [
" !nosetests tests/test_type_util.py -v "
]
2015-01-26 22:00:11 +08:00
}
2015-05-14 18:55:50 +08:00
] ,
" 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 " ,
2015-06-19 09:07:36 +08:00
" version " : " 2.7.10 "
2015-05-14 18:55:50 +08:00
}
} ,
" nbformat " : 4 ,
" nbformat_minor " : 0
}