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 2a0c45f5 authored by Franck Thollard's avatar Franck Thollard
Browse files

removing unittest, using assert

parent d3201417
Pipeline #33143 passed with stage
in 1 minute and 1 second
...@@ -31,8 +31,6 @@ ...@@ -31,8 +31,6 @@
"\n", "\n",
"Coding without testing is dangerous (anyway, you will test somehow): \n", "Coding without testing is dangerous (anyway, you will test somehow): \n",
"\n", "\n",
"https://lesjoiesducode.fr/quand-le-client-veut-une-mise-en-prod-avant-de-boucler-ses-tests\n",
"\n",
"https://lesjoiesducode.fr/quand-je-dploie-en-prod-sans-tester\n", "https://lesjoiesducode.fr/quand-je-dploie-en-prod-sans-tester\n",
"\n" "\n"
] ]
...@@ -50,8 +48,7 @@ ...@@ -50,8 +48,7 @@
"Your get more confident with your code: \n", "Your get more confident with your code: \n",
"\n", "\n",
"https://lesjoiesducode.fr/quand-jai-blinde-mes-tests-et-que-les-recetteurs-commencent-a-passer-sur-mes-devs\n", "https://lesjoiesducode.fr/quand-jai-blinde-mes-tests-et-que-les-recetteurs-commencent-a-passer-sur-mes-devs\n",
"\n", "\n"
"https://thecodinglove.com/when-i-run-my-code-for-the-first-time-and-i-didnt-test-anything\n"
] ]
}, },
{ {
...@@ -64,11 +61,12 @@ ...@@ -64,11 +61,12 @@
"source": [ "source": [
"## Why testing (3/4) ? \n", "## Why testing (3/4) ? \n",
"\n", "\n",
"To get sure I conform with the specs, and/or define correct specs: \n", "To get sure I conform with the specs, and/or define correct specs. \n",
"Important when something went wrong ... \n",
"\n", "\n",
"https://thecodinglove.com/solid-code-wrong-specs\n", "https://thecodinglove.com/solid-code-wrong-specs\n",
"\n", "\n",
"ease parallel dev. and code integration." "Ease parallel dev. and code integration."
] ]
}, },
{ {
...@@ -250,6 +248,10 @@ ...@@ -250,6 +248,10 @@
"\n", "\n",
"Different possibilities \n", "Different possibilities \n",
"\n", "\n",
"- just use assert ! \n",
"\n",
"Not detailed here:\n",
"\n",
"- use unittest from the standard lib \n", "- use unittest from the standard lib \n",
"\n", "\n",
"- use pytest (rather standard, but need to be installed)\n", "- use pytest (rather standard, but need to be installed)\n",
...@@ -257,9 +259,49 @@ ...@@ -257,9 +259,49 @@
"- ...." "- ...."
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Assert ? \n",
"\n",
"assert test a condition and \"crashes\" if the condition does not evaluate to True"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"assert 13 == 10+3\n",
"assert type(10) is int"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-10-bf474889e709>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0;32mTrue\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"assert True is False"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 3,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "slide" "slide_type": "slide"
...@@ -267,21 +309,16 @@ ...@@ -267,21 +309,16 @@
}, },
"outputs": [ "outputs": [
{ {
"name": "stderr", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"test_add (__main__.TestMyStuff)\n", "testing add with int .... OK\n",
"test add ... ok\n", "testing add ok with str... OK\n",
"\n", "test add ok\n"
"----------------------------------------------------------------------\n",
"Ran 1 test in 0.002s\n",
"\n",
"OK\n"
] ]
} }
], ],
"source": [ "source": [
"import unittest \n",
"\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",
...@@ -290,15 +327,17 @@ ...@@ -290,15 +327,17 @@
" result = arg0 + arg1\n", " result = arg0 + arg1\n",
" return result\n", " return result\n",
"\n", "\n",
"\n", "def test_add():\n",
"class TestMyStuff(unittest.TestCase): \n", " \"\"\" test add is ok with int and strings\"\"\"\n",
" def test_add(self):\n", " print(\"testing add with int \", end=\"\")\n",
" \"\"\" test add \"\"\"\n", " assert add(1, 2) == 3\n",
" self.assertEqual(8, add(3, 5))\n", " print(\" .... OK\")\n",
" self.assertEqual(\"aabb\", add(\"aa\", \"bb\"))\n", " print(\"testing add ok with str\", end=\"\")\n",
" # self.assertEqual(None, add(None, None))\n", " assert add(\"a\", \"b\") == \"ab\"\n",
"\n", " print(\"... OK\")\n",
"_res = unittest.TextTestRunner(verbosity=2).run(unittest.TestLoader().loadTestsFromTestCase(TestMyStuff))" " print(\"test add ok\")\n",
"\n",
"test_add()"
] ]
}, },
{ {
...@@ -370,34 +409,30 @@ ...@@ -370,34 +409,30 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stderr", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"test_add_second_twice (__main__.TestMyStuff0)\n", "testing add second twice with int "
"test add_second_twice ... FAIL\n", ]
"\n", },
"======================================================================\n", {
"FAIL: test_add_second_twice (__main__.TestMyStuff0)\n", "ename": "AssertionError",
"test add_second_twice\n", "evalue": "",
"----------------------------------------------------------------------\n", "output_type": "error",
"Traceback (most recent call last):\n", "traceback": [
" File \"<ipython-input-3-1ec00cf044ac>\", line 17, in test_add_second_twice\n", "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
" self.assertEqual(13, add_second_twice(3, 5))\n", "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"AssertionError: 13 != None\n", "\u001b[0;32m<ipython-input-5-84287d31dec0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"test add second twice OK with int, string and list\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 26\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 27\u001b[0;31m \u001b[0mtest_add_second_twice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\n", "\u001b[0;32m<ipython-input-5-84287d31dec0>\u001b[0m in \u001b[0;36mtest_add_second_twice\u001b[0;34m()\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\"\"\" test add second twice\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"testing add second twice with int \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0madd_second_twice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m13\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"...OK\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"testing add second twice with strings \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"----------------------------------------------------------------------\n", "\u001b[0;31mAssertionError\u001b[0m: "
"Ran 1 test in 0.006s\n",
"\n",
"FAILED (failures=1)\n"
] ]
} }
], ],
"source": [ "source": [
"import unittest\n",
"\n", "\n",
"def add_second_twice(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",
...@@ -410,14 +445,20 @@ ...@@ -410,14 +445,20 @@
" pass\n", " pass\n",
"\n", "\n",
"\n", "\n",
"class TestMyStuff0(unittest.TestCase): \n", "def test_add_second_twice():\n",
" def test_add_second_twice(self):\n", " \"\"\" test add second twice\"\"\"\n",
" \"\"\" test add_second_twice\"\"\"\n", " print(\"testing add second twice with int \", end=\"\")\n",
" self.assertEqual(13, add_second_twice(3, 5))\n", " assert add_second_twice(3, 5) == 13\n",
" self.assertEqual(\"aabbbb\", add_second_twice(\"aa\", \"bb\"))\n", " print(\"...OK\")\n",
" self.assertListEqual([1, 2, 3, 4, 3, 4], add_second_twice([1, 2], [3, 4])) \n", " print(\"testing add second twice with strings \", end=\"\")\n",
" \n", " assert add_second_twice(\"aa\", \"bb\") == \"aabbbb\"\n",
"_res = unittest.TextTestRunner(verbosity=2).run(unittest.TestLoader().loadTestsFromTestCase(TestMyStuff0))" " print(\"...OK\")\n",
" print(\"testing add second twice with list \", end=\"\")\n",
" assert add_second_twice([1,2], [3,4]) == [1, 2, 3, 4, 3, 4]\n",
" print(\"...OK\")\n",
" print(\"test add second twice OK with int, string and list\")\n",
"\n",
"test_add_second_twice()"
] ]
}, },
{ {
...@@ -435,40 +476,24 @@ ...@@ -435,40 +476,24 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 6,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "-" "slide_type": "-"
} }
}, },
"outputs": [ "outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"test_add_second_twice (__main__.TestMyStuff0)\n",
"test add_second_twice ... "
]
},
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"arg0 + 2*arg1 = 3 + 2*5 = 13\n", "testing add second twice with int arg0 + 2*arg1 = 3 + 2*5 = 13\n",
"arg0 + 2*arg1 = aa + 2*bb = aabbbb\n", "...OK\n",
"arg0 + 2*arg1 = [1, 2] + 2*[3, 4] = [1, 2, 3, 4, 3, 4]\n" "testing add second twice with strings arg0 + 2*arg1 = aa + 2*bb = aabbbb\n",
] "...OK\n",
}, "testing add second twice with list arg0 + 2*arg1 = [1, 2] + 2*[3, 4] = [1, 2, 3, 4, 3, 4]\n",
{ "...OK\n",
"name": "stderr", "test add second twice OK with int, string and list\n"
"output_type": "stream",
"text": [
"ok\n",
"\n",
"----------------------------------------------------------------------\n",
"Ran 1 test in 0.003s\n",
"\n",
"OK\n"
] ]
} }
], ],
...@@ -487,15 +512,20 @@ ...@@ -487,15 +512,20 @@
" 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",
"\n", "\n",
"\n", "def test_add_second_twice():\n",
"class TestMyStuff0(unittest.TestCase): \n", " \"\"\" test add second twice\"\"\"\n",
" def test_add_second_twice(self):\n", " print(\"testing add second twice with int \", end=\"\")\n",
" \"\"\" test add_second_twice\"\"\"\n", " assert add_second_twice(3, 5) == 13\n",
" self.assertEqual(13, add_second_twice(3, 5))\n", " print(\"...OK\")\n",
" self.assertEqual(\"aabbbb\", add_second_twice(\"aa\", \"bb\"))\n", " print(\"testing add second twice with strings \", end=\"\")\n",
" self.assertListEqual([1, 2, 3, 4, 3, 4], add_second_twice([1, 2], [3, 4])) \n", " assert add_second_twice(\"aa\", \"bb\") == \"aabbbb\"\n",
" \n", " print(\"...OK\")\n",
"_res = unittest.TextTestRunner(verbosity=2).run(unittest.TestLoader().loadTestsFromTestCase(TestMyStuff0))\n" " print(\"testing add second twice with list \", end=\"\")\n",
" assert add_second_twice([1,2], [3,4]) == [1, 2, 3, 4, 3, 4]\n",
" print(\"...OK\")\n",
" print(\"test add second twice OK with int, string and list\")\n",
"\n",
"test_add_second_twice()"
] ]
}, },
{ {
......
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