Skip to content

Commit 2eb5abb

Browse files
committed
Add final results
1 parent ed0b625 commit 2eb5abb

1 file changed

Lines changed: 66 additions & 115 deletions

File tree

‎questionnaire_unit_test.md‎

Lines changed: 66 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,32 @@ Uses the 'unittest' automated framework but not only to test questionnaire.py
77
```commandline
88
python ./questionnaire_unit_tests.py
99
```
10+
Note : All tests are described following the format :\
11+
`test number`->`returned values` if receives __argument(s)__ then prints `message`
12+
1013
---
1114
### Level 1 : Basic Unit tests about the arguments received by questionnaire.py
1215

1316
the argument are read and handled by the `load_json_argv` function which should return the json data `<Json_Data>`
1417
and the file path `<File_Path>`
15-
16-
`test number`->`returned values` if receives __condition given in argument__ and prints `message`
18+
`
1719

18-
- `test_01` -> `<Json_Data>, <File_Path>` if receives __a good json file__ and prints no message
19-
```commandline
20-
questionnaire.py initial_json_test_file.json -> Json_Data, File_Path
21-
```
20+
- `test_01` -> `<Json_Data>, <File_Path>` if receives __a good json file__ then prints no message
2221

23-
- `test_02` -> `None, None` if receives __no file__ and prints a message.
24-
```commandline
25-
questionnaire.py -> None, None
26-
Error : Error in argument, should add a single .json questionnaire. Example : questionnaire.py questionnaire_to_read.json
27-
```
22+
- `test_02` -> `None, None` if receives __no file__ then prints the message :
23+
```Error : Error in argument, should add a single .json questionnaire```
2824

29-
- `test_03` -> `None, None` if receives __more than one file__ and prints a message.
30-
```commandline
31-
questionnaire.py initial_json_test_file.json.json second_json_test_file.json -> None, None
32-
Error : Error in argument, should add a single .json questionnaire. Example : questionnaire.py questionnaire_to_read.json
33-
```
25+
- `test_03` -> `None, None` if receives __more than one file__ then prints the message :
26+
```Error : Error in argument, should add a single .json questionnaire```
3427

35-
- `test_04` -> `None, <File_Path>` if receives __a file without json extension__ and prints a message.
36-
```commandline
37-
questionnaire.py json_file_without_extension -> None, File_Path
38-
Error : Incorrect extension, add a .json questionnaire. Example : questionnaire.py questionnaire_to_read.json
39-
```
28+
- `test_04` -> `None, <File_Path>` if receives __a file without json extension__ then prints the message :
29+
```Error : Incorrect extension, add a .json questionnaire```
4030

41-
- `test_05` -> `None, <File_Path>` if receives __a non-existent file__ and prints a message.
42-
```commandline
43-
questionnaire.py non-existent-file.json -> None, File_Path
44-
Error : Invalid permissions or path to non-existent-file.json
45-
```
31+
- `test_05` -> `None, <File_Path>` if receives __a non-existent file__ then prints the message :
32+
```Error : Invalid permissions or path to non-existent-file.json```
4633

47-
- `test_06` -> `None, <File_Path>` if receives __an empty json file__ and prints a message.
48-
```commandline
49-
questionnaire.py empty.json -> None, File_Path
50-
Error : Incompatible or no data in JSON file empty.json
51-
```
34+
- `test_06` -> `None, <File_Path>` if receives __an empty json file__ then prints the message :
35+
```Error : Incompatible or no data in JSON file empty.json```
5236

5337
---
5438
### Level 2 : Same tests but with a different name of program
@@ -62,161 +46,128 @@ The class derives from the level 1 and executes the same tests except that
6246
a [json_file_tests](./json_file_tests) folder is added to the path of the file given in argument
6347

6448
The printed message of test 2 is quite different from the Level 1 since the file path is given in argument.
65-
- `test_02` -> `None, <File_Path>` if receives __no file__ and prints a message
66-
```commandline
67-
questionnaire.py json_file_tests/ -> None, File_Path
68-
Error : Incorrect extension, add a .json questionnaire. Example : questionnaire.py questionnaire_to_read.json
69-
```
49+
- `test_02` -> `None, <File_Path>` if receives __no file__ then prints a message
50+
```Error : Incorrect extension, add a .json questionnaire.```
51+
7052
---
7153
### Level 4 : Same tests but with another name and a file into a folder
7254

7355
The class derives from the level 2 and 3 and executes the same tests under `test.py` name and with files
7456
from [json_file_tests](./json_file_tests)
7557

7658
The printed message of test 2 is so quite different from the Level 1 as well.
77-
- `test_02` -> `None, <File_Path>` if receives __no file__ and prints a message.
78-
```commandline
79-
test.py json_file_tests/ -> None, File_Path
80-
Error : Incorrect extension, add a .json questionnaire. Example : test.py questionnaire_to_read.json
81-
```
59+
- `test_02` -> `None, <File_Path>` if receives __no file__ then prints the message :
60+
```Error : Incorrect extension, add a .json questionnaire.```
8261

8362
---
8463
### Level 5 : Tests with data online
8564

8665
A free json file is available at the url : https://countwordsfree.com/example.json
8766

88-
- `test_01` -> `<Json_Data>, <File_Path>` if receives __a valid json file on a valid url path__ and prints no message.
89-
```commandline
90-
questionnaire.py https://countwordsfree.com/example.json -> Json_Data, File_Path
91-
```
67+
- `test_01` -> `<Json_Data>, <File_Path>` if receives __a valid json file on a valid url path__ then prints no message.
9268

93-
- `test_02` -> `None, <File_Path>` if receives __a invalid json file on a valid url path__ and prints a message.
94-
```commandline
95-
questionnaire.py https://countwordsfree.com/non-existant.json -> None, File_Path
96-
Error : File not found at URL https://countwordsfree.com/non-existant.json
97-
```
9869

99-
- `test_03` -> `None, <File_Path>` if receives __a valid json file but on a invalid url path__ and prints a message.
100-
```commandline
101-
questionnaire.py https://countwordsfre.com/example.json -> None, File_Path
102-
Error : Invalid URL at https://countwordsfre.com/example.json
103-
```
70+
- `test_02` -> `None, <File_Path>` if receives __a invalid json file on a valid url path__ then prints the message :\
71+
```Error : File not found at URL https://countwordsfree.com/non-existant.json```
72+
73+
74+
- `test_03` -> `None, <File_Path>` if receives __a valid json file but on a invalid url path__ then prints the message :\
75+
```Error : Invalid URL at https://countwordsfre.com/example.json```
10476

105-
- `test_04` -> `None, <File_Path>` if receives __a file without json extension on a valid url path__ and prints a message.
106-
```commandline
107-
questionnaire.py https://countwordsfree.com/empty_or_uncompleted_file -> None, File_Path
108-
Error : Incorrect extension, add a .json questionnaire. Example : questionnaire.py questionnaire_to_read.json
109-
```
77+
78+
- `test_04` -> `None, <File_Path>` if receives __a file without json extension on a valid url path__ then prints the message :\
79+
```Error : Incorrect extension, add a .json questionnaire.```
11080

11181
---
11282
### Level 6 : Tests with data on http://localhost
11383

11484
To go further and test wrong or empty json files, it is necessary to simulate a simple HTTP server
11585
via localhost(`http://127.0.0.1`) for example.
11686

117-
The simple HTTP server is started through a thread by the `setUp` method and close by the `tearDown` method. Those
118-
2 `unittest.TestCase` methods are respectively called before and after each test of the class and can be overwritten.
87+
The simple HTTP server is started through a thread by the `setUp` method and close by the `tearDown` method.\
88+
Those 2 `unittest.TestCase` methods are respectively called before and after each test of the class and can be overwritten.
11989

12090
The class derives from the level 1 and therefore executes the same tests but through an HTTP connection.
12191

122-
- `test_01` -> `<Json_Data>, <File_Path>` if receives __a good json file__ and prints no message.
123-
```commandline
124-
questionnaire.py http://127.0.0.1:8000/initial_json_test_file.json -> Json_Data, File_Path
125-
127.0.0.1 - - [02/Dec/2022 12:11:53] "GET /initial_json_test_file.json HTTP/1.1" 200 -
126-
```
92+
- `test_01` -> `<Json_Data>, <File_Path>` if receives __a good json file__ then prints no message.
93+
12794

12895
The printed message of test 2 is quite different from the Level 1 since the URL is given in argument.
129-
- `test_02` -> `None, <File_Path>` if receives __no file__ and prints a message
130-
```commandline
131-
questionnaire.py http://127.0.0.1:8000/ -> None, File_Path
132-
Error : Incorrect extension, add a .json questionnaire. Example : questionnaire.py questionnaire_to_read.json
133-
```
96+
- `test_02` -> `None, <File_Path>` if receives __no file__ then prints a message \
97+
```Error : Incorrect extension, add a .json questionnaire```
13498

135-
- `test_03` -> `None, None` if receives __more than one file__ and prints a message.
136-
```commandline
137-
questionnaire.py http://127.0.0.1:8000/initial_json_test_file.json.json http://127.0.0.1:8000/second_json_test_file.json -> None, None
138-
Error : Error in argument, should add a single .json questionnaire. Example : questionnaire.py questionnaire_to_read.json
139-
```
99+
Idem for test 5 and 6 because the argument given here is a URL and not a file.
100+
101+
- `test_05` -> `None, File_Path` if receives __a non-existent file__ then prints the message :\
102+
```Error : File not found at URL <URL>```
103+
104+
105+
- `test_06` -> `None, File_Path` if receives __an empty json file__ then prints the message :\
106+
```Error : Incompatible or no data in JSON file at URL <URL>```
140107

141-
- `test_04` -> `None, File_Path` if receives __a file without json extension__ and prints a message.
142-
```commandline
143-
questionnaire.py http://127.0.0.1:8000/json_file_without_extension -> None, File_Path
144-
Error : Incorrect extension, add a .json questionnaire. Example : questionnaire.py questionnaire_to_read.json
145-
```
146108

147-
- `test_05` -> `None, File_Path` if receives __a non-existent file__ and prints a message.
148-
```commandline
149-
questionnaire.py http://127.0.0.1:8000/non-existent-file.json -> None, File_Path
150-
Error : File not found at URL http://127.0.0.1:8000/non-existent-file.json
151-
127.0.0.1 - - [02/Dec/2022 12:22:31] code 404, message File not found
152-
127.0.0.1 - - [02/Dec/2022 12:22:31] "GET /non-existent-file.json HTTP/1.1" 404 -
153-
```
154-
- `test_0x` -> `None, File_Path` if receives __an empty json file__ and prints a message.
155-
```commandline
156-
questionnaire.py http://127.0.0.1:8000/empty.json -> None, File_Path
157-
Error : Incompatible or no data in JSON file at URL http://127.0.0.1:8000/empty.json
158-
127.0.0.1 - - [02/Dec/2022 12:22:31] "GET /empty.json HTTP/1.1" 200 -
159-
```
160109
---
161110
### Level 7 : Optimized Level 6 with pytest
162111

163112
Rather than opening and closing an HTTP server for each test, it would be better to open and close it once for all tests
164113
in the class.
165114

166-
This needs to create a fixture defined on the `class` scope and to use the option `autouse=True`
115+
This can be done by creating a fixture defined on the `class` scope and using the option `autouse=True` to automatically
116+
applied it to all the tests of the parent class.
167117

168118
The fixture will so open the server, before all the tests run, and will be close the server when it will be called a
169119
second time after that all tests ran.
170120
```commandline
171121
HTTP Connection to http://127.0.0.1:8000/ in Thread-4 (serve_forever)
172-
questionnaire.py http://127.0.0.1:8000/json_file_tests/initial_json_test_file.json -> Json_Data, File_Path
173-
127.0.0.1 - - [05/Dec/2022 15:28:35] "GET /json_file_tests/initial_json_test_file.json HTTP/1.1" 200 -
174-
PASSED
175-
questionnaire_unit_test.py::_07_load_json_argv_with_data_via_localhost_8000_in_fixture::test_02_load_json_argv_with_no_argv
176-
questionnaire.py http://127.0.0.1:8000/json_file_tests/ -> None, File_Path
177-
Error : Incorrect extension, add a .json questionnaire. Example : questionnaire.py questionnaire_to_read.json
178-
PASSED
122+
...
123+
<tests>
124+
...
179125
HTTP Disconnection from http://127.0.0.1:8000/
180126
```
181-
According to the definition of the fixture used in this test, this class will be executed only using `pytest` and
127+
Note : According to the definition of the fixture used in this test, this class will be executed only using `pytest` and
182128
will be skipped under `unittests`
183129
```commandline
184130
pytest -v -k questionnaire_unit_test.py -s
185131
```
132+
186133
---
187-
### Level 8 : Optimized Level 6 with unittests `@classmethod`
134+
### Level 8 : Optimized Level 6 with unittests built-in `@classmethod` tag
188135

189136
In the same idea as Level 7 with fixtures, it is possible to call the built-in methods `setUpClass` and `tearDownClass`
190137
at the beginning and at the end of the tests of the class.\
191138
To do this, the `self` argument must be modified to `cls` because these built-in functions are private to the unittest
192-
class. It is also why it is also necessary to use the built-in tag `@classmethod` in order to access them.
139+
class. It is also why it is also necessary to use the built-in `@classmethod` tag in order to access them.
193140

194141
---
195142
### Level 9 : Tests with a patch on network requests
196143

197-
Unit tests should be isolated and not depend on the system or the network in order to be able, for example, to measure
144+
Finally, unit tests should be isolated and not depend on the system or the network in order to be able, for example, to measure
198145
exactly the time needed to proceed without a external dependency.
199146

200147
It should so better to simulate the answers of the network requests like ``Success (200)``
201-
or ``File Not Found (404)`` for getting the best knowledge of the program behavior.
148+
or ``File Not Found (404)`` for example, to get the best knowledge of the program behavior.
202149

203150
This may require to rewrite the tests because the reading of the file from URL must also be simulated, like in this 2
204151
tests trying to read files from a non-existent URL of the Web.
205152

206153
- `test_01` -> `None, File_Path` if receives __a json file on a non-existent URL with a simulated ``File Not Found (404)`` answer to
207-
the network request__ and prints a message.
154+
the network request__ then prints the message :
208155
```commandline
209156
questionnaire.py http://non-existent-url.com/initial_json_test_file.json -> None, File_Path
210157
Error : File not found at URL http://non-existent-url.com/initial_json_test_file.json
211158
```
212-
- `test_02` -> `not None` if receives __a json file on a non-existent URL with a simulated ``Success (200)`` answer to the
213-
network request__ and prints no message.
159+
- `test_02` -> `Json_Data, File_Path` if receives __a json file on a non-existent URL with a simulated ``Success (200)`` answer to the
160+
network request__ then prints no message.
214161
```commandline
215-
questionnaire.py http://non-existent-url.com/initial_json_test_file.json -> Not None (OK)
162+
questionnaire.py http://non-existent-url.com/initial_json_test_file.json -> Json_Data, File_Path
216163
```
217-
in that case, the property ``status_code`` of the response must be updated to the code ``200 (OK)``
218-
and the property ``text`` of the response must contain the json data read. This is simulated by the reading of a file
219-
from the given path without the server part ``./initial_json_test_file.json``
164+
In that case, the property `status_code` of the response must be updated to the code `200 (OK)` and the property `text`
165+
of the response must contain the read json data. This is simulated by the reading of a file from the given path without
166+
the server part ``./initial_json_test_file.json``
167+
168+
---
169+
### Level 10 : Json Tests
170+
220171

221172
---
222173
### More Unit Tests Documentations

0 commit comments

Comments
 (0)