Commit 28ca6ad1 authored by paugier's avatar paugier
Browse files

Use assert instead of unittest + improve presentation on for loops

parent eabc7703
Pipeline #32528 passed with stage
in 52 seconds
This diff is collapsed.
......@@ -224,24 +224,20 @@
nb_elem = 0
for e in arg0:
if e == elem:
nb_elem += 1
return nb_elem
```
import unittest
class TestMyStuff(unittest.TestCase):
def test_myfunc1(self):
""" test myfunc1 """
self.assertEqual(3, my_count("abb"))
self.assertEqual(1, my_count("abb", "a"))
self.assertEqual(2, my_count("abb", "b"))
self.assertEqual(0, my_count(""))
self.assertEqual(0, my_count("abb", "c"))
self.assertEqual(None, None)
_res = unittest.TextTestRunner(verbosity=2).run(unittest.TestLoader().loadTestsFromTestCase(TestMyStuff))
%% Cell type:code id: tags:
``` python
assert my_count("abb") == 3
assert my_count("abb", "a") == 1
assert my_count("abb", "b") == 2
assert my_count("") == 0
assert my_count("abb", "c") == 0
```
%% Cell type:markdown id: tags:
### Default arguments
......@@ -289,36 +285,21 @@
if l is None:
l = []
l.append(1)
print(l)
return l
import unittest
class TestMyStuff(unittest.TestCase):
def test_myfunc1(self):
""" test myfunc1 """
l1 = [1, 2, 3]
how_to_use_list_as_default_arg(l1)
self.assertEqual(l1, [1, 2, 3, 1])
l = how_to_use_list_as_default_arg()
self.assertEqual(l, [1])
_res = unittest.TextTestRunner(verbosity=2).run(unittest.TestLoader().loadTestsFromTestCase(TestMyStuff))
print("running function")
how_to_use_list_as_default_arg()
how_to_use_list_as_default_arg()
how_to_use_list_as_default_arg()
l1 = [1,2,3]
how_to_use_list_as_default_arg(l1)
l1
```
%%%% Output: execute_result
%% Cell type:code id: tags:
[1, 2, 3, 1]
``` python
l1 = [1, 2, 3]
how_to_use_list_as_default_arg(l1)
assert l1 == [1, 2, 3, 1]
assert how_to_use_list_as_default_arg() == [1]
assert how_to_use_list_as_default_arg() == [1]
```
%% Cell type:markdown id: tags:
### Variable-length arguments
......@@ -376,29 +357,31 @@
def multiply(l, factor=2):
""" nput a list l and a number a and that multiplies all the elements
of the list by the number. If not set, number is defaulted to 2."""
pass
import unittest
class TestMyStuff(unittest.TestCase):
def test_multiply(self):
""" test multiply """
import traceback
def test_func(func):
try:
l1 = [1, 2]
multiply(l1)
self.assertEqual([2, 4], l1)
multiply(l1, 4)
self.assertEqual([8, 16], l1)
func(l1)
assert l1 == [2, 4]
func(l1, 4)
assert l1 == [8, 16]
l1 = []
multiply(l1)
self.assertEqual([], l1)
func(l1)
assert l1 == []
l1 = ['a', 'b']
multiply(l1)
self.assertEqual(["aa", "bb"], l1)
_res = unittest.TextTestRunner(verbosity=2).run(unittest.TestLoader().loadTestsFromTestCase(TestMyStuff))
func(l1)
assert l1 == ["aa", "bb"]
except AssertionError:
traceback.print_exc()
else:
print("All tests pass!")
test_func(multiply)
```
%% Cell type:markdown id: tags:
#### A possible solution:
......@@ -408,40 +391,11 @@
``` python
def multiply(l, a=2):
for i, val in enumerate(l):
l[i] = a * val
import unittest
class TestMyStuff(unittest.TestCase):
def test_multiply(self):
""" test multiply """
l1 = [1, 2]
multiply(l1)
self.assertEqual([2, 4], l1)
multiply(l1, 4)
self.assertEqual([8, 16], l1)
l1 = []
multiply(l1)
self.assertEqual([], l1)
l1 = ['a', 'b']
multiply(l1)
self.assertEqual(["aa", "bb"], l1)
_res = unittest.TextTestRunner(verbosity=2).run(unittest.TestLoader().loadTestsFromTestCase(TestMyStuff))
l = list(range(3))
print(l)
func(l)
print(l)
func(l, 4)
print(l)
l = ['a', 'b']
func(l, 4)
print(l)
test_func(multiply)
```
%% Cell type:markdown id: tags:
## `lambda` keyword and anonymous functions
......
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