Skip to content

Commit 6b294d0

Browse files
add test m1 019
1 parent 17549a3 commit 6b294d0

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

projects/m1/019-average/README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Average
22

3-
In this exercise you will create a program that computes the average of a collection of values entered by the user.
4-
The user will enter 0 as a sentinel value to indicate that no further values will be provided. Your program should display an appropriate
5-
error message if the first value entered by the user is 0.
3+
In this exercise you will create a program that **computes the average of a collection of values entered by the user**.
4+
The user will enter **0 as a sentinel value** to indicate that no further values will be provided.
65

7-
Hint: Because the 0 marks the end of the input it should not be included in the average.
6+
Your program should **display an appropriate error message if the first value entered by the user is 0**.
7+
**Hint**: Because the 0 marks the end of the input it should not be included in the average.
88

99

1010
# Documentation
@@ -16,6 +16,25 @@ For this project solution you may use:
1616
- Iteration
1717
- Strings
1818

19+
20+
# Test
21+
Execute the test to validate your solution.
22+
23+
**VSCODE**
24+
To run the test command from the README.md install the extension **runme**.
25+
Press Ctrl+Shift+x search and install the **runme** extension.
26+
27+
28+
**Python**
29+
30+
```sh
31+
python -m unittest python/test_average.py
32+
```
33+
34+
or run the command from the terminal
35+
`python -m unittest projects/m1/019-average/python/test_average.py`
36+
37+
1938
# Deadline
2039

2140
This project requires to be completed in a maximum of **1 hour**

projects/m1/019-average/__init__.py

Whitespace-only changes.

projects/m1/019-average/python/__init__.py

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import importlib
2+
import io
3+
import os
4+
import re
5+
import sys
6+
from pathlib import Path
7+
from unittest import TestCase, skipIf
8+
from unittest.mock import patch
9+
10+
file_path = Path(__file__).absolute().parent / 'main.py'
11+
is_file_empty = os.stat(file_path).st_size == 0
12+
13+
14+
@skipIf(is_file_empty, 'Empty file. Test 019 Skipped')
15+
class TestNextDay(TestCase):
16+
17+
def setUp(self) -> None:
18+
self.module_name = 'projects.m1.019-average.python.main'
19+
20+
@patch('builtins.input')
21+
def test_ok(self, mock_inputs):
22+
"""
23+
Check if return the correct result
24+
"""
25+
26+
mock_inputs.side_effect = [50, 2, 3, 10, 0]
27+
28+
with patch('sys.stdout', new_callable=io.StringIO) as mock_print:
29+
sys.modules.pop(self.module_name, None)
30+
importlib.import_module(name=self.module_name, package='files')
31+
32+
result = float(re.findall(r'\d+\.\d+', mock_print.getvalue())[-1])
33+
self.assertEqual(16.25, result)
34+

0 commit comments

Comments
 (0)