Skip to content
Snippets Groups Projects
Commit 387b0ddb authored by Sven Graupner's avatar Sven Graupner
Browse files

new Assignment_C README.md, images

parent f9cf9878
No related branches found
No related tags found
No related merge requests found
......@@ -131,5 +131,7 @@ dmypy.json
# project-specific files
README_init.md
**/*_sol.py
# solution files and .py files in project directory
**/*_sol.py
/*.py
# Assignment C: Python Expressions &nbsp; (<span style="color:red">16 Pts</span>)
This assignment demonstrates Python's powerful (*"one-liner"*) expressions.
### Challenges
1. [Challenge 1:](#1-challenge-1-create-new-project) Create New Project
1. [Challenge 2:](#2-challenge-2-run-code) Run Code
1. [Challenge 3:](#3-challenge-3-run-unit-tests) Run Unit Tests
1. [Challenge 4:](#4-challenge-4-write-expressions) Write Expressions
1. [Challenge 5:](#5-challenge-5-final-test) Final Test
&nbsp;
### 1.) Challenge 1: Create New Project
Create a Python project, e.g. named `C_expressions`, and
[pull files](https://gitlab.bht-berlin.de/sgraupner/ds_cs4bd_2324/-/tree/main/C_expressions)
from GitLab.
Inspect files and figure out their purpose. Write 1-2 sentenses what each file means
and purpose is:
- [__init __.py](https://gitlab.bht-berlin.de/sgraupner/ds_cs4bd_2324/-/blob/main/C_expressions/__init__.py)
: `_____________________________________`
- What does the init-file contain?
- When and how often is this file executed?
- [expressions.py](https://gitlab.bht-berlin.de/sgraupner/ds_cs4bd_2324/-/blob/main/C_expressions/expressions.py)
: `__________________________________`
- [test_expressions.py](https://gitlab.bht-berlin.de/sgraupner/ds_cs4bd_2324/-/blob/main/C_expressions/test_expressions.py)
: `______________________________`
(1 Pt)
&nbsp;
### 2.) Challenge 2: Run Code
Run file `expressions.py` in your IDE:
```
numbers: [4, 12, 3, 8, 17, 12, 1, 8, 7]
#
a) number of numbers: 9
b) first three numbers: []
c) last three numbers: []
d) last three numbers reverse: []
e) odd numbers: []
f) number of odd numbers: 0
g) sum of odd numbers: 0
h) duplicate numbers removed: []
i) number of duplicate numbers: 0
j) ascending, de-dup (n^2) numbers: []
k) length: NEITHER
[Done] exited with code=0 in 0.126 seconds
```
(1 Pt)
Run file `expressions.py` in terminal:
```sh
cd <project> # cd into project directory
pwd # print working directory
/c/.../workspaces/ds_cs4bd_2324/C_expressions
python expressions.py # run program
-->
numbers: [4, 12, 3, 8, 17, 12, 1, 8, 7]
a) number of numbers: 9
b) first three numbers: []
c) last three numbers: []
d) last three numbers reverse: []
e) odd numbers: []
f) number of odd numbers: 0
g) sum of odd numbers: 0
h) duplicate numbers removed: []
i) number of duplicate numbers: 0
j) ascending, de-dup (n^2) numbers: []
k) length: NEITHER
```
(1 Pt)
&nbsp;
### 3.) Challenge 3: Run Unit Tests
Unit Tests are used to *"test-a-unit"* of code in isolation. This unit can be
a function, a file, a class, a module.
In contrast to running code regularly, Unit Tests execute under the
supervision of a `test runner` that:
- looks for (discovers) tested units,
- executes them with test data, collects test results regardless
whether a test succeeded or failed and
- reports test results at the and.
Read *"A Beginner’s Guide to Unit Tests in Python"*,
[link](https://www.dataquest.io/blog/unit-tests-python/),
and answer questions:
- How are tests discovered? Which feature makes the test runner to collect
something as a test?
- What is an
[assert](https://docs.python.org/3/library/unittest.html#assert-methods)
statement? What happens when a test (assert) passes and fails?
- Where is the test runner started in given files?
(1 Pt)
Run tests in a terminal. Currently, only one test runs and passes:
*TestCase_a_number_of_numbers* :
```sh
python test_expressions.py # run tests directly from file calling the
# test runner in __main__
```
Output:
```
test_a_number_of_numbers (C_expressions.test_expressions.TestCase_a_number_of_nu
mbers.test_a_number_of_numbers) ... ok
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
<unittest.runner.TextTestResult run=1 errors=0 failures=0>
```
Result: 1 test was performed that passed.
Alternatively, run tests with test discovery. Run the unit test module that
starts the test runner, which in turn discovers tests that are then executed:
```sh
python -m unittest # let test runner discover tests
```
Output is the same as above.
(1 Pt)
Configure your IDE so it also runs Unit Tests. VSCode discovers unit tests under
the test glass icon (red circled).
The figure shows one unit test that has been discovered passing. Unit tests are
structured as *"TestCase - Classes"*, which are classes that inherit from class:
[unittest.TestCase](https://docs.python.org/3/library/unittest.html#unittest.TestCase),
in the example indirectly through class `Test_case_a`.
VSCode shows discovered test classes in the left panel and their execution result
with a green check mark when passed or a red cross when failed.
![](../markup/img/C_unit_tests_1.png)
Uncomment tests: *"Test_case_b"* and *"Test_case_c"* in `test_expressions.py`
above and re-run tests.
Both tests should fail because expressions they test have not been implemented:
![](../markup/img/C_unit_tests_2.png)
Re-run unit tests with the two tests failing in the terminal:
```sh
python -m unittest # let test runner discover tests
```
Output shows one passing and two failed tests:
```
======================================================================
FAIL: test_b_first_three_numbers (test_expressions.TestCase_b_first_three_number
s.test_b_first_three_numbers)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Sven1\svgr\workspaces\ds_cs4bd_2324\C_expressions\test_expressions.py
", line 103, in test_b_first_three_numbers
self.assertEqual(self.ut1.b, [4, 12, 3])
AssertionError: Lists differ: [] != [4, 12, 3]
Second list contains 3 additional elements.
First extra element 0:
4
- []
+ [4, 12, 3]
======================================================================
FAIL: test_c_last_three_numbers (test_expressions.TestCase_c_last_three_numbers.
test_c_last_three_numbers)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Sven1\svgr\workspaces\ds_cs4bd_2324\C_expressions\test_expressions.py
", line 117, in test_c_last_three_numbers
td.assertEqual(td.ut1.c, [1, 8, 7])
AssertionError: Lists differ: [] != [1, 8, 7]
Second list contains 3 additional elements.
First extra element 0:
1
- []
+ [1, 8, 7]
----------------------------------------------------------------------
Ran 3 tests in 0.002s
FAILED (failures=2)
```
Output says: `Ran 3 tests`, `FAILED (failures=2)`.
When tests fail, the test report tells which tests have failed and why:
- *test_b_first_three_numbers* failed in line: 103. The test expected
result: `[4, 12, 3]`, but an empty list `[]` was found in the tested
expression: `self.b` in file `expressions.py`.
- *test_c_last_three_numbers* failed in line: 117 where the test expected
result: `[1, 8, 7]`, but an empty list `[]` was found in: `self.c`
Tests refer to the `self.numbers` list: `[4, 12, 3, 8, 17, 12, 1, 8, 7]`.
(1 Pt)
&nbsp;
### 4.) Challenge 4: Write Expressions
In order to let tests pass, write expressions in
[expressions.py](https://gitlab.bht-berlin.de/sgraupner/ds_cs4bd_2324/-/blob/main/C_expressions/expressions.py)
for variables `self.b` .. `self.k` according to specification, e.g. write an
expression for `self.b` that extracts the first three numbers from `self.numbers`.
Use <b>one-line expressions</b> only. Built-in functions are allowed, but not
own functions.
Tests exercise expressions with various lists. Initialization with constants
(`self.b = [4, 12, 3]`) will hence not work.
Write expression incrementally, one after the other - not all at once. Some
expressions require thinking and reading.
Once you have written an expression, uncomment the corresponding test case in
[test_expressions.py](https://gitlab.bht-berlin.de/sgraupner/ds_cs4bd_2324/-/blob/main/C_expressions/test_expressions.py):
and re-run the test. See if it is passing or figure out why it is failing
from the test report.
![](../markup/img/C_unit_tests_3.png)
Test cases a), b) and c) are now passing.
Continue until all tests pass.
![](../markup/img/C_unit_tests_4.png)
&nbsp;
### 5.) Challenge 5: Final Test
Pull latest Unit-test file from GitLab and re-run tests:
```sh
wget https://gitlab.bht-berlin.de/sgraupner/ds_cs4bd_2324/-/blob/main/C_express
ions/test_expressions.py
python test_expressions.py --tests=all
```
Result:
```
...........
----------------------------------------------------------------------
Ran 11 tests in 0.002s
OK
<unittest.runner.TextTestResult run=11 errors=0 failures=0>
```
(10 Pts, 1 Pt for each test passing)
Run unit tests with discovery (-m) or from __main__() with verbosity level 2
- python -m unittest test_expressions.py
- python test_expressions.py
Output with verbosity level < 2:
================================
...........
----------------------------------------------------------------------
Ran 11 tests in 0.002s
OK
Output with verbosity level >=2:
================================
test_a_number_of_numbers (C_expressions.test_expressions.TestCase_a_number_of_numbers.test_a_number_of_numbers) ... ok
test_b_first_three_numbers (C_expressions.test_expressions.TestCase_b_first_three_numbers.test_b_first_three_numbers) ... ok
test_c_last_three_numbers (C_expressions.test_expressions.TestCase_c_last_three_numbers.test_c_last_three_numbers) ... ok
test_d_last_threeClass_in_reverse (C_expressions.test_expressions.TestCase_d_last_threeClass_in_reverse.test_d_last_threeClass_in_reverse) ... ok
test_e_odd_numbers (C_expressions.test_expressions.TestCase_e_odd_numbers.test_e_odd_numbers) ... ok
test_f_number_of_odd_numbers (C_expressions.test_expressions.TestCase_f_number_of_odd_numbers.test_f_number_of_odd_numbers) ... ok
test_g_sum_of_odd_numbers (C_expressions.test_expressions.TestCase_g_sum_of_odd_numbers.test_g_sum_of_odd_numbers) ... ok
test_h_duplicateClass_removed (C_expressions.test_expressions.TestCase_h_duplicateClass_removed.test_h_duplicateClass_removed) ... ok
test_i_number_of_duplicate_numbers (C_expressions.test_expressions.TestCase_i_number_of_duplicate_numbers.test_i_number_of_duplicate_numbers) ... ok
test_j_ascending_squaredClass_no_duplicates (C_expressions.test_expressions.TestCase_j_ascending_squaredClass_no_duplicates.test_j_ascending_squaredClass_no_duplicates) ... ok
test_k_classifyClass_as_odd_even_empty (C_expressions.test_expressions.TestCase_k_classifyClass_as_odd_even_empty.test_k_classifyClass_as_odd_even_empty) ... ok
----------------------------------------------------------------------
Ran 11 tests in 0.007s
OK
<unittest.runner.TextTestResult run=11 errors=0 failures=0>
......@@ -5,6 +5,7 @@ This repository contains the assignments for class
- Assignment A - [Python setup](A_setup_python).
- Assignment B - [Explore Python](B_explore_python).
- Assignment C - [Explore Python](C_expressions).
&nbsp;
......
markup/img/C_unit_tests_1.png

187 KiB

markup/img/C_unit_tests_2.png

157 KiB

markup/img/C_unit_tests_3.png

160 KiB

markup/img/C_unit_tests_4.png

223 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment