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