Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
python-uga
py-training-2017
Commits
2a0c45f5
Commit
2a0c45f5
authored
Dec 08, 2019
by
Franck Thollard
Browse files
removing unittest, using assert
parent
d3201417
Pipeline
#33143
passed with stage
in 1 minute and 1 second
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
ipynb/pres031_functions_testing.ipynb
View file @
2a0c45f5
...
...
@@ -14,12 +14,10 @@
## Why testing (1/4) ?
Coding without testing is dangerous (anyway, you will test somehow):
https://lesjoiesducode.fr/quand-le-client-veut-une-mise-en-prod-avant-de-boucler-ses-tests
https://lesjoiesducode.fr/quand-je-dploie-en-prod-sans-tester
%% Cell type:markdown id: tags:
...
...
@@ -28,22 +26,22 @@
Your get more confident with your code:
https://lesjoiesducode.fr/quand-jai-blinde-mes-tests-et-que-les-recetteurs-commencent-a-passer-sur-mes-devs
https://thecodinglove.com/when-i-run-my-code-for-the-first-time-and-i-didnt-test-anything
%% Cell type:markdown id: tags:
## Why testing (3/4) ?
To get sure I conform with the specs, and/or define correct specs:
To get sure I conform with the specs, and/or define correct specs.
Important when something went wrong ...
https://thecodinglove.com/solid-code-wrong-specs
e
ase parallel dev. and code integration.
E
ase parallel dev. and code integration.
%% Cell type:markdown id: tags:
## Why testing (4/4) ?
...
...
@@ -159,37 +157,70 @@
### How to test ?
Different possibilities
-
just use assert !
Not detailed here:
-
use unittest from the standard lib
-
use pytest (rather standard, but need to be installed)
-
....
%% Cell type:markdown id: tags:
### Assert ?
assert test a condition and "crashes" if the condition does not evaluate to True
%% Cell type:code id: tags:
```
python
assert
13
==
10
+
3
assert
type
(
10
)
is
int
```
%% Cell type:code id: tags:
```
python
assert
True
is
False
```
%%%% Output: error
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-10-bf474889e709> in <module>
----> 1 assert True is False
AssertionError:
%% Cell type:code id: tags:
```
python
import
unittest
def
add
(
arg0
,
arg1
):
"""Print and return the sum of the two arguments (duck typing).
assuming arg0 + arg1 is well defined.
"""
result
=
arg0
+
arg1
return
result
def
test_add
():
""" test add is ok with int and strings"""
print
(
"testing add with int "
,
end
=
""
)
assert
add
(
1
,
2
)
==
3
print
(
" .... OK"
)
print
(
"testing add ok with str"
,
end
=
""
)
assert
add
(
"a"
,
"b"
)
==
"ab"
print
(
"... OK"
)
print
(
"test add ok"
)
class
TestMyStuff
(
unittest
.
TestCase
):
def
test_add
(
self
):
""" test add """
self
.
assertEqual
(
8
,
add
(
3
,
5
))
self
.
assertEqual
(
"aabb"
,
add
(
"aa"
,
"bb"
))
# self.assertEqual(None, add(None, None))
_res
=
unittest
.
TextTestRunner
(
verbosity
=
2
).
run
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestMyStuff
))
test_add
()
```
%% Cell type:code id: tags:
```
python
...
...
@@ -217,11 +248,10 @@
Write a function that returns the sum of the first argument with twice the second argument.
%% Cell type:code id: tags:
```
python
import
unittest
def
add_second_twice
(
arg0
,
arg1
):
"""Return the sum of the first argument with twice the second one.
Arguments should be of type that support sum and product by
an integer (e.g. numerical, string, list, ...)
...
...
@@ -230,20 +260,43 @@
:return: arg0 + 2 * arg1
"""
pass
class
TestMyStuff0
(
unittest
.
TestCase
):
def
test_add_second_twice
(
self
):
""" test add_second_twice"""
self
.
assertEqual
(
13
,
add_second_twice
(
3
,
5
))
self
.
assertEqual
(
"aabbbb"
,
add_second_twice
(
"aa"
,
"bb"
))
self
.
assertListEqual
([
1
,
2
,
3
,
4
,
3
,
4
],
add_second_twice
([
1
,
2
],
[
3
,
4
]))
_res
=
unittest
.
TextTestRunner
(
verbosity
=
2
).
run
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestMyStuff0
))
def
test_add_second_twice
():
""" test add second twice"""
print
(
"testing add second twice with int "
,
end
=
""
)
assert
add_second_twice
(
3
,
5
)
==
13
print
(
"...OK"
)
print
(
"testing add second twice with strings "
,
end
=
""
)
assert
add_second_twice
(
"aa"
,
"bb"
)
==
"aabbbb"
print
(
"...OK"
)
print
(
"testing add second twice with list "
,
end
=
""
)
assert
add_second_twice
([
1
,
2
],
[
3
,
4
])
==
[
1
,
2
,
3
,
4
,
3
,
4
]
print
(
"...OK"
)
print
(
"test add second twice OK with int, string and list"
)
test_add_second_twice
()
```
%%%% Output: error
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-5-84287d31dec0> in <module>
25 print("test add second twice OK with int, string and list")
26
---> 27 test_add_second_twice()
<ipython-input-5-84287d31dec0> in test_add_second_twice()
15 """ test add second twice"""
16 print("testing add second twice with int ", end="")
---> 17 assert add_second_twice(3, 5) == 13
18 print("...OK")
19 print("testing add second twice with strings ", end="")
AssertionError:
%% Cell type:markdown id: tags:
### Do it yourself: simple function definition
**A solution:**
...
...
@@ -263,20 +316,24 @@
"""
result
=
arg0
+
2
*
arg1
print
(
f
'arg0 + 2*arg1 =
{
arg0
}
+ 2*
{
arg1
}
=
{
result
}
'
)
return
result
def
test_add_second_twice
():
""" test add second twice"""
print
(
"testing add second twice with int "
,
end
=
""
)
assert
add_second_twice
(
3
,
5
)
==
13
print
(
"...OK"
)
print
(
"testing add second twice with strings "
,
end
=
""
)
assert
add_second_twice
(
"aa"
,
"bb"
)
==
"aabbbb"
print
(
"...OK"
)
print
(
"testing add second twice with list "
,
end
=
""
)
assert
add_second_twice
([
1
,
2
],
[
3
,
4
])
==
[
1
,
2
,
3
,
4
,
3
,
4
]
print
(
"...OK"
)
print
(
"test add second twice OK with int, string and list"
)
class
TestMyStuff0
(
unittest
.
TestCase
):
def
test_add_second_twice
(
self
):
""" test add_second_twice"""
self
.
assertEqual
(
13
,
add_second_twice
(
3
,
5
))
self
.
assertEqual
(
"aabbbb"
,
add_second_twice
(
"aa"
,
"bb"
))
self
.
assertListEqual
([
1
,
2
,
3
,
4
,
3
,
4
],
add_second_twice
([
1
,
2
],
[
3
,
4
]))
_res
=
unittest
.
TextTestRunner
(
verbosity
=
2
).
run
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestMyStuff0
))
test_add_second_twice
()
```
%% Cell type:code id: tags:
```
python
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment