Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

Commit 81c9fcd8 authored by Franck Thollard's avatar Franck Thollard
Browse files

using unittest for the DIY

parent 29b47f03
...@@ -88,13 +88,53 @@ ...@@ -88,13 +88,53 @@
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 8,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"test_add (__main__.TestMyStuff0)\n",
"test max_temp computes correctly ... "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"result = 5\n",
"result = aabb\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"ok\n",
"\n",
"----------------------------------------------------------------------\n",
"Ran 1 test in 0.004s\n",
"\n",
"OK\n"
]
}
],
"source": [ "source": [
"import unittest\n",
"\n",
"def add(arg0, arg1):\n", "def add(arg0, arg1):\n",
" \"\"\"Print and return the sum of the two arguments (duck typing).\"\"\"\n", " \"\"\"Print and return the sum of the two arguments (duck typing).\"\"\"\n",
" result = arg0 + arg1\n", " result = arg0 + arg1\n",
" print('result = ', result)\n", " print('result = ', result)\n",
" return result" " return result\n",
"\n",
"\n",
"class TestMyStuff(unittest.TestCase): \n",
" def test_add(self):\n",
" \"\"\" test max_temp computes correctly\"\"\"\n",
" self.assertEqual(8, add(3, 5))\n",
" self.assertEqual(\"aabb\", add(\"aa\", \"bb\"))\n",
"\n",
"_res = unittest.TextTestRunner(verbosity=2).run(unittest.TestLoader().loadTestsFromTestCase(TestMyStuff0))"
] ]
}, },
{ {
...@@ -164,6 +204,65 @@ ...@@ -164,6 +204,65 @@
"Write a function that returns the sum of the first argument with twice the second argument." "Write a function that returns the sum of the first argument with twice the second argument."
] ]
}, },
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"test_add_second_twice (__main__.TestMyStuff0)\n",
"test max_temp computes correctly ... "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"arg0 + 2*arg1 = 3 + 2*5 = 13\n",
"arg0 + 2*arg1 = aa + 2*bb = aabbbb\n",
"arg0 + 2*arg1 = [1, 2] + 2*[3, 4] = [1, 2, 3, 4, 3, 4]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"ok\n",
"\n",
"----------------------------------------------------------------------\n",
"Ran 1 test in 0.003s\n",
"\n",
"OK\n"
]
}
],
"source": [
"import unittest\n",
"\n",
"def add_second_twice(arg0, arg1):\n",
" \"\"\"Return the sum of the first argument with twice the second one.\n",
" Arguments should be of type that support sum and product by \n",
" an integer (e.g. numerical, string, list, ...)\n",
" :param arg0: first argument\n",
" :param arg1: second argument \n",
" :return: arg0 + 2 * arg1\n",
" \"\"\"\n",
" pass\n",
"\n",
"\n",
"class TestMyStuff0(unittest.TestCase): \n",
" def test_add_second_twice(self):\n",
" \"\"\" test max_temp computes correctly\"\"\"\n",
" self.assertEqual(13, add_second_twice(3, 5))\n",
" self.assertEqual(\"aabbbb\", add_second_twice(\"aa\", \"bb\"))\n",
" self.assertListEqual([1, 2, 3, 4, 3, 4], add_second_twice([1, 2], [3, 4])) \n",
" \n",
"_res = unittest.TextTestRunner(verbosity=2).run(unittest.TestLoader().loadTestsFromTestCase(TestMyStuff0))"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
...@@ -205,8 +304,14 @@ ...@@ -205,8 +304,14 @@
} }
], ],
"source": [ "source": [
"def myfunc(arg0, arg1):\n", "def add_second_twice(arg0, arg1):\n",
" \"Return the sum of the first argument with twice the second one.\"\n", " \"\"\"Return the sum of the first argument with twice the second one.\n",
" Arguments should be of type that support sum and product by \n",
" an integer (e.g. numerical, string, list, ...)\n",
" :param arg0: first argument\n",
" :param arg1: second argument \n",
" :return: arg0 + 2 * arg1\n",
" \"\"\"\n",
" result = arg0 + 2*arg1\n", " result = arg0 + 2*arg1\n",
" print(f'arg0 + 2*arg1 = {arg0} + 2*{arg1} = {result}')\n", " print(f'arg0 + 2*arg1 = {arg0} + 2*{arg1} = {result}')\n",
" return result\n", " return result\n",
...@@ -375,27 +480,29 @@ ...@@ -375,27 +480,29 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"[4] 2\n" "das f [17, 4]\n",
"[17] 2\n"
] ]
} }
], ],
"source": [ "source": [
"number = 2\n", "number = 2\n",
"mylist = []\n", "mylist = [17]\n",
"\n", "\n",
"def my_strange_append_square(mylist, number):\n", "def my_strange_append_square(mylist, number):\n",
" # new function namespace with names \"mylist\" and \"number\"\n", " # new function namespace with names \"mylist\" and \"number\"\n",
" number = number**2\n", " number = number**2\n",
" mylist.append(number)\n", " mylist.append( number)\n",
" print(\"das f\", mylist)\n",
" \n", " \n",
"my_strange_append_square(mylist, number)\n", "my_strange_append_square(mylist[:], number)\n",
"print(mylist, number)" "print(mylist, number)"
] ]
}, },
...@@ -551,7 +658,7 @@ ...@@ -551,7 +658,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 7,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -567,7 +674,7 @@ ...@@ -567,7 +674,7 @@
"16" "16"
] ]
}, },
"execution_count": 15, "execution_count": 7,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -648,23 +755,28 @@ ...@@ -648,23 +755,28 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 12,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"arg0 + 2*arg1 = 1 + 2*0 = 1\n" "arg0 + 2*arg1 = a + 2*n = ann\n"
] ]
} }
], ],
"source": [ "source": [
"def myfunc1(arg0, arg1=0):\n", "def myfunc1(arg0, arg1=None):\n",
" \"Return the sum of the first argument with twice the second one.\"\n", " \"Return the sum of the first argument with twice the second one.\"\n",
" if arg1 is None:\n",
" if type(arg0) == int:\n",
" arg1 = 0\n",
" else:\n",
" arg1 = \"\"\n",
" myfunc(arg0, arg1)\n", " myfunc(arg0, arg1)\n",
"\n", "\n",
"myfunc1(1)" "myfunc1(\"a\", \"n\")"
] ]
}, },
{ {
...@@ -699,18 +811,18 @@ ...@@ -699,18 +811,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 19, "execution_count": 15,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "name": "stdout",
"text/plain": [ "output_type": "stream",
"([],)" "text": [
] "[1]\n",
}, "[1, 1]\n",
"execution_count": 19, "[1, 1, 1]\n",
"metadata": {}, "[1, 1, 1, 1]\n"
"output_type": "execute_result" ]
} }
], ],
"source": [ "source": [
...@@ -718,7 +830,10 @@ ...@@ -718,7 +830,10 @@
" l.append(1)\n", " l.append(1)\n",
" print(l)\n", " print(l)\n",
"\n", "\n",
"do_not_use_mutable_object_for_default_arg.__defaults__" "do_not_use_mutable_object_for_default_arg()\n",
"do_not_use_mutable_object_for_default_arg()\n",
"do_not_use_mutable_object_for_default_arg()\n",
"do_not_use_mutable_object_for_default_arg()"
] ]
}, },
{ {
...@@ -777,7 +892,7 @@ ...@@ -777,7 +892,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 16,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -786,8 +901,19 @@ ...@@ -786,8 +901,19 @@
"text": [ "text": [
"[1]\n", "[1]\n",
"[1]\n", "[1]\n",
"[1]\n" "[1]\n",
"[1, 2, 3, 1]\n"
] ]
},
{
"data": {
"text/plain": [
"[1, 2, 3, 1]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
} }
], ],
"source": [ "source": [
...@@ -799,7 +925,10 @@ ...@@ -799,7 +925,10 @@
"\n", "\n",
"how_to_use_list_as_default_arg()\n", "how_to_use_list_as_default_arg()\n",
"how_to_use_list_as_default_arg()\n", "how_to_use_list_as_default_arg()\n",
"how_to_use_list_as_default_arg()" "how_to_use_list_as_default_arg()\n",
"l1 = [1,2,3]\n",
"how_to_use_list_as_default_arg(l1)\n",
"l1"
] ]
}, },
{ {
...@@ -875,7 +1004,7 @@ ...@@ -875,7 +1004,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24, "execution_count": 19,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -884,17 +1013,17 @@ ...@@ -884,17 +1013,17 @@
"text": [ "text": [
"call:\n", "call:\n",
"\ta = 1\n", "\ta = 1\n",
"\targs = (2, 3)\n", "\targs = (3,)\n",
"\tkwargs = {'toto': 3, 'titi': 3}\n", "\tkwargs = {'toto': 3, 'titi': 3}\n",
"call:\n", "call:\n",
"\ta = a\n", "\ta = a\n",
"\targs = ('b',)\n", "\targs = ()\n",
"\tkwargs = {'bob': 3}\n" "\tkwargs = {'bob': 3}\n"
] ]
} }
], ],
"source": [ "source": [
"def func(a, *args, **kwargs):\n", "def func(a, b, *args, **kwargs):\n",
" print(f'call:\\n\\ta = {a}\\n\\targs = {args}\\n\\tkwargs = {kwargs}')\n", " print(f'call:\\n\\ta = {a}\\n\\targs = {args}\\n\\tkwargs = {kwargs}')\n",
" \n", " \n",
"func(1, 2, 3, toto=3, titi=3)\n", "func(1, 2, 3, toto=3, titi=3)\n",
...@@ -915,6 +1044,103 @@ ...@@ -915,6 +1044,103 @@
"all the elements of the list by the number. If not set, number is defaulted to 2." "all the elements of the list by the number. If not set, number is defaulted to 2."
] ]
}, },
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"test_mult_list_int_defaut (__main__.TestMyStuff0) ... FAIL\n",
"test_mult_list_int_with_factor (__main__.TestMyStuff0) ... FAIL\n",
"test_mult_list_str_with_factor (__main__.TestMyStuff0) ... FAIL\n",
"\n",
"======================================================================\n",
"FAIL: test_mult_list_int_defaut (__main__.TestMyStuff0)\n",
"----------------------------------------------------------------------\n",
"Traceback (most recent call last):\n",
" File \"<ipython-input-23-4858bef3312c>\", line 14, in test_mult_list_int_defaut\n",
" self.assertListEqual(l_target, l)\n",
"AssertionError: Lists differ: [2, 6, 10] != [1, 3, 5]\n",
"\n",
"First differing element 0:\n",
"2\n",
"1\n",
"\n",
"- [2, 6, 10]\n",
"+ [1, 3, 5]\n",
"\n",
"======================================================================\n",
"FAIL: test_mult_list_int_with_factor (__main__.TestMyStuff0)\n",
"----------------------------------------------------------------------\n",
"Traceback (most recent call last):\n",
" File \"<ipython-input-23-4858bef3312c>\", line 8, in test_mult_list_int_with_factor\n",
" self.assertListEqual(l_target, l)\n",
"AssertionError: Lists differ: [2, 6, 10] != [1, 3, 5]\n",
"\n",
"First differing element 0:\n",
"2\n",
"1\n",
"\n",
"- [2, 6, 10]\n",
"+ [1, 3, 5]\n",
"\n",
"======================================================================\n",
"FAIL: test_mult_list_str_with_factor (__main__.TestMyStuff0)\n",
"----------------------------------------------------------------------\n",
"Traceback (most recent call last):\n",
" File \"<ipython-input-23-4858bef3312c>\", line 20, in test_mult_list_str_with_factor\n",
" self.assertListEqual(l_target, l)\n",
"AssertionError: Lists differ: ['aa', 'bb', 'cc'] != ['a', 'b', 'c']\n",
"\n",
"First differing element 0:\n",
"'aa'\n",
"'a'\n",
"\n",
"- ['aa', 'bb', 'cc']\n",
"? - - -\n",
"\n",
"+ ['a', 'b', 'c']\n",
"\n",
"----------------------------------------------------------------------\n",
"Ran 3 tests in 0.007s\n",
"\n",
"FAILED (failures=3)\n"
]
}
],
"source": [
"import unittest\n",
"\n",
"class TestMyStuff0(unittest.TestCase): \n",
" def test_mult_list_int_with_factor(self):\n",
" l = [1, 3, 5]\n",
" l_target = [2, 6, 10]\n",
" mult_list(l, 2)\n",
" self.assertListEqual(l_target, l)\n",
" \n",
" def test_mult_list_int_defaut(self):\n",
" l = [1, 3, 5]\n",
" l_target = [2, 6, 10]\n",
" mult_list(l, 2)\n",
" self.assertListEqual(l_target, l)\n",
" \n",
" def test_mult_list_str_with_factor(self):\n",
" l = [\"a\", \"b\", \"c\"]\n",
" l_target = [\"aa\", \"bb\", \"cc\"]\n",
" mult_list(l, 2)\n",
" self.assertListEqual(l_target, l)\n",
"\n",
"def mult_list(my_list, factor):\n",
" \"Multiply each element of my_list by factor (default=2)\"\n",
" pass\n",
"\n",
"\n",
"_res = unittest.TextTestRunner(verbosity=2).run(unittest.TestLoader().loadTestsFromTestCase(TestMyStuff0))"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
...@@ -928,15 +1154,34 @@ ...@@ -928,15 +1154,34 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 25, "execution_count": 22,
"metadata": { "metadata": {},
"collapsed": true "outputs": [
}, {
"outputs": [], "name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2]\n",
"[0, 2, 4]\n",
"[0, 8, 16]\n",
"['aaaa', 'bbbb']\n"
]
}
],
"source": [ "source": [
"def func(l, a=2):\n", "def func(l, a=2):\n",
" for i, val in enumerate(l):\n", " for i, val in enumerate(l):\n",
" l[i] = a * val" " l[i] = a * val\n",
" \n",
"l = list(range(3))\n",
"print(l)\n",
"func(l)\n",
"print(l)\n",
"func(l, 4)\n",
"print(l)\n",
"l = ['a', 'b']\n",
"func(l, 4)\n",
"print(l)\n"
] ]
}, },
{ {
...@@ -1094,7 +1339,7 @@ ...@@ -1094,7 +1339,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.6.6" "version": "3.7.3rc1"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment