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 @@
"\n",
"Coding without testing is dangerous (anyway, you will test somehow): \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",
"\n"
]
......@@ -50,8 +48,7 @@
"Your get more confident with your code: \n",
"\n",
"https://lesjoiesducode.fr/quand-jai-blinde-mes-tests-et-que-les-recetteurs-commencent-a-passer-sur-mes-devs\n",
"\n",
"https://thecodinglove.com/when-i-run-my-code-for-the-first-time-and-i-didnt-test-anything\n"
"\n"
]
},
{
......@@ -64,11 +61,12 @@
"source": [
"## Why testing (3/4) ? \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",
"https://thecodinglove.com/solid-code-wrong-specs\n",
"\n",
"ease parallel dev. and code integration."
"Ease parallel dev. and code integration."
]
},
{
......@@ -250,6 +248,10 @@
"\n",
"Different possibilities \n",
"\n",
"- just use assert ! \n",
"\n",
"Not detailed here:\n",
"\n",
"- use unittest from the standard lib \n",
"\n",
"- use pytest (rather standard, but need to be installed)\n",
......@@ -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",
"execution_count": 2,
"execution_count": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
......@@ -267,21 +309,16 @@
},
"outputs": [
{
"name": "stderr",
"name": "stdout",
"output_type": "stream",
"text": [
"test_add (__main__.TestMyStuff)\n",
"test add ... ok\n",
"\n",
"----------------------------------------------------------------------\n",
"Ran 1 test in 0.002s\n",
"\n",
"OK\n"
"testing add with int .... OK\n",
"testing add ok with str... OK\n",
"test add ok\n"
]
}
],
"source": [
"import unittest \n",
"\n",
"def add(arg0, arg1):\n",
" \"\"\"Print and return the sum of the two arguments (duck typing).\n",
......@@ -290,15 +327,17 @@
" result = arg0 + arg1\n",
" return result\n",
"\n",
"\n",
"class TestMyStuff(unittest.TestCase): \n",
" def test_add(self):\n",
" \"\"\" test add \"\"\"\n",
" self.assertEqual(8, add(3, 5))\n",
" self.assertEqual(\"aabb\", add(\"aa\", \"bb\"))\n",
" # self.assertEqual(None, add(None, None))\n",
"\n",
"_res = unittest.TextTestRunner(verbosity=2).run(unittest.TestLoader().loadTestsFromTestCase(TestMyStuff))"
"def test_add():\n",
" \"\"\" test add is ok with int and strings\"\"\"\n",
" print(\"testing add with int \", end=\"\")\n",
" assert add(1, 2) == 3\n",
" print(\" .... OK\")\n",
" print(\"testing add ok with str\", end=\"\")\n",
" assert add(\"a\", \"b\") == \"ab\"\n",
" print(\"... OK\")\n",
" print(\"test add ok\")\n",
"\n",
"test_add()"
]
},
{
......@@ -370,34 +409,30 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"name": "stdout",
"output_type": "stream",
"text": [
"test_add_second_twice (__main__.TestMyStuff0)\n",
"test add_second_twice ... FAIL\n",
"\n",
"======================================================================\n",
"FAIL: test_add_second_twice (__main__.TestMyStuff0)\n",
"test add_second_twice\n",
"----------------------------------------------------------------------\n",
"Traceback (most recent call last):\n",
" File \"<ipython-input-3-1ec00cf044ac>\", line 17, in test_add_second_twice\n",
" self.assertEqual(13, add_second_twice(3, 5))\n",
"AssertionError: 13 != None\n",
"\n",
"----------------------------------------------------------------------\n",
"Ran 1 test in 0.006s\n",
"\n",
"FAILED (failures=1)\n"
"testing add second twice with int "
]
},
{
"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-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",
"\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",
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"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",
......@@ -410,14 +445,20 @@
" pass\n",
"\n",
"\n",
"class TestMyStuff0(unittest.TestCase): \n",
" def test_add_second_twice(self):\n",
" \"\"\" test add_second_twice\"\"\"\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))"
"def test_add_second_twice():\n",
" \"\"\" test add second twice\"\"\"\n",
" print(\"testing add second twice with int \", end=\"\")\n",
" assert add_second_twice(3, 5) == 13\n",
" print(\"...OK\")\n",
" print(\"testing add second twice with strings \", end=\"\")\n",
" assert add_second_twice(\"aa\", \"bb\") == \"aabbbb\"\n",
" 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 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"test_add_second_twice (__main__.TestMyStuff0)\n",
"test add_second_twice ... "
]
},
{
"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"
"testing add second twice with int arg0 + 2*arg1 = 3 + 2*5 = 13\n",
"...OK\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",
"test add second twice OK with int, string and list\n"
]
}
],
......@@ -487,15 +512,20 @@
" print(f'arg0 + 2*arg1 = {arg0} + 2*{arg1} = {result}')\n",
" return result\n",
"\n",
"\n",
"class TestMyStuff0(unittest.TestCase): \n",
" def test_add_second_twice(self):\n",
" \"\"\" test add_second_twice\"\"\"\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))\n"
"def test_add_second_twice():\n",
" \"\"\" test add second twice\"\"\"\n",
" print(\"testing add second twice with int \", end=\"\")\n",
" assert add_second_twice(3, 5) == 13\n",
" print(\"...OK\")\n",
" print(\"testing add second twice with strings \", end=\"\")\n",
" assert add_second_twice(\"aa\", \"bb\") == \"aabbbb\"\n",
" 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()"
]
},
{
......
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