-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_program.py
414 lines (297 loc) · 9.28 KB
/
test_program.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import pytest
from program import *
# Subject class test cases
def test_subject_class():
# Test if the subject class is defined
obj = Subject(4, 10)
assert obj.credit == 4
assert obj.grade_point == 10
obj = Subject(3, 9)
assert obj.credit == 3
assert obj.grade_point == 9
# raise an error if the credit is not an integer
with pytest.raises(TypeError):
obj = Subject(3.5, 9)
with pytest.raises(TypeError):
obj = Subject('three', 9)
with pytest.raises(TypeError):
obj = Subject(True, 9)
# raise an error if the grade_point is not an integer
with pytest.raises(TypeError):
obj = Subject(3, 9.5)
with pytest.raises(TypeError):
obj = Subject(3, 'nine')
with pytest.raises(TypeError):
obj = Subject(3, True)
# raise an error if the credit and grade point is not negative
with pytest.raises(ValueError):
obj = Subject(-3, 9)
with pytest.raises(ValueError):
obj = Subject(3, -9)
with pytest.raises(ValueError):
obj = Subject(-3, -9)
# GPA class test cases
# addsubject function test cases
def test_addsubject():
# create an object of the gpa class
obj = Gpa()
# Test if the addsubject function is defined
obj.addsubject(4, 10)
assert obj.no_of_subjects == 1
assert obj.sub[0].credit == 4
assert obj.sub[0].grade_point == 10
obj.addsubject(3, 9)
assert obj.no_of_subjects == 2
assert obj.sub[1].credit == 3
assert obj.sub[1].grade_point == 9
def test_invalid_credit():
with pytest.raises(TypeError):
obj = Subject(3.5, 9)
with pytest.raises(TypeError):
obj = Subject('three', 9)
with pytest.raises(TypeError):
obj = Subject(True, 9)
def test_invalid_grade_point():
with pytest.raises(TypeError):
obj = Subject(3, 9.5)
with pytest.raises(TypeError):
obj = Subject(3, 'nine')
with pytest.raises(TypeError):
obj = Subject(3, True)
def test_invalid_credit_and_grade_point():
obj = Gpa()
# raise an error if the credit and grade point is not negative
with pytest.raises(ValueError):
obj.addsubject(-3, 9)
with pytest.raises(ValueError):
obj.addsubject(3, -9)
with pytest.raises(ValueError):
obj.addsubject(-3, -9)
def test_credit_greater_than_8():
obj = Gpa()
# raise an error if the credit is greater than 8
with pytest.raises(ValueError):
obj.addsubject(9, 9)
with pytest.raises(ValueError):
obj.addsubject(13, 9)
def test_grade_point_greater_than_10():
obj = Gpa()
# raise an error if the grade point is greater than 10
with pytest.raises(ValueError):
obj.addsubject(3, 11)
with pytest.raises(ValueError):
obj.addsubject(3, 15)
# calc function test cases
def test_calc_1():
obj = Gpa()
# Test if the calc function is defined
obj.addsubject(4, 10)
obj.addsubject(3, 9)
obj.addsubject(3, 8)
obj.addsubject(2, 7)
obj.addsubject(2, 8)
obj.addsubject(1, 9)
obj.addsubject(3, 9)
obj.addsubject(2, 3)
obj.calc()
assert obj.no_of_subjects == 8
assert obj.gpa == 8.722222222222221
def test_calc_2():
obj = Gpa()
# Test if the calc function is defined
obj.addsubject(4, 10)
obj.addsubject(3, 10)
obj.addsubject(3, 10)
obj.addsubject(2, 10)
obj.addsubject(2, 10)
obj.addsubject(1, 10)
obj.addsubject(3, 10)
obj.addsubject(2, 10)
obj.addsubject(2, 10)
obj.calc()
assert obj.gpa == 10
def test_calc_3():
obj = Gpa()
# Test if the calc function is defined
obj.addsubject(4, 7)
obj.addsubject(3, 7)
obj.addsubject(3, 7)
obj.addsubject(2, 7)
obj.addsubject(2, 7)
obj.addsubject(1, 7)
obj.addsubject(3, 7)
obj.addsubject(2, 7)
obj.addsubject(2, 7)
obj.calc()
assert obj.gpa == 7
def test_calc_4():
obj = Gpa()
# Test if the calc function is defined
obj.addsubject(2, 8)
obj.addsubject(3, 8)
obj.addsubject(3, 7)
obj.addsubject(4, 8)
obj.addsubject(2, 10)
obj.addsubject(4, 9)
obj.addsubject(4, 9)
obj.addsubject(3, 8)
obj.calc()
assert obj.gpa == 8.36
def test_calc_with_no_subjects():
obj = Gpa()
# Test if the calc function is defined
with pytest.raises(ValueError):
obj.calc()
assert obj.gpa == 0.0
# display function test cases
def test_display(capsys):
obj = Gpa()
# Test if the display function is defined
obj.addsubject(4, 10)
obj.addsubject(3, 9)
obj.addsubject(3, 8)
obj.addsubject(2, 7)
obj.addsubject(2, 7)
obj.addsubject(1, 9)
obj.addsubject(3, 8)
obj.addsubject(2, 3)
# calculate and call the display function
obj.calc()
obj.display()
# capture the output(prints)
captured = capsys.readouterr()
# check if the output is correct
assert captured.out == "No of subjects: 8\nGPA: 8.444444444444445\n"
def test_display_with_no_subjects():
obj = Gpa()
# Test if the display function is defined
with pytest.raises(ValueError):
obj.display()
assert obj.gpa == 0.0
def test_display_with_no_calc():
obj = Gpa()
# Test if the display function is defined
obj.addsubject(4, 10)
obj.addsubject(3, 9)
obj.addsubject(3, 8)
obj.addsubject(2, 7)
obj.addsubject(2, 7)
obj.addsubject(1, 9)
obj.addsubject(3, 8)
obj.addsubject(2, 3)
with pytest.raises(ValueError):
obj.display()
assert obj.gpa == 0.0
# gpa class overall test cases
def test_gpa_class(capsys):
obj = Gpa()
# add subjects
credits = [2, 3, 3, 4, 2, 4, 4, 3]
grade_points = [8, 8, 7, 8, 10, 9, 9, 8]
for i in range(len(credits)):
obj.addsubject(credits[i], grade_points[i])
# check if the number of subjects is correct
assert obj.no_of_subjects == 8
# check if subjects are added to list
assert len(obj.sub) == 8
# check if subjects are added correctly
for i in range(len(obj.sub)):
assert obj.sub[i].credit == credits[i]
assert obj.sub[i].grade_point == grade_points[i]
# calculate
obj.calc()
# display
obj.display()
# capture the output(prints)
captured = capsys.readouterr()
# check if the output is correct
assert captured.out == "No of subjects: 8\nGPA: 8.36\n"
# check if the gpa is correct
assert obj.gpa == 8.36
# cgpa class test cases
# addsemester function test cases
def test_addsemester():
obj = Cgpa()
gpa_obj = Gpa()
# add subjects
credits = [2, 3, 3, 4, 2, 4, 4, 3]
grade_points = [8, 8, 7, 8, 10, 9, 9, 8]
for i in range(len(credits)):
gpa_obj.addsubject(credits[i], grade_points[i])
# Test if the addsemester function is defined
obj.addsemester(gpa_obj)
# check if the semester is added to the list
assert len(obj.semester) == 1
# check if the semester is added correctly
assert obj.semester[0].gpa == 8.36
def test_invalid_addsemester():
obj = Cgpa()
# Test if the addsemester function is defined
with pytest.raises(TypeError):
obj.addsemester(-1)
with pytest.raises(TypeError):
obj.addsemester(15)
with pytest.raises(TypeError):
obj.addsemester('hello')
with pytest.raises(TypeError):
obj.addsemester(3.14)
def test_addsemester_with_no_subjects():
obj = Cgpa()
gpa_obj = Gpa()
# Test if the addsemester function is defined
with pytest.raises(ValueError):
obj.addsemester(gpa_obj)
assert len(obj.semester) == 0
def test_addsemester_with_fail_gradepoints():
obj = Cgpa()
gpa_obj = Gpa()
# add subjects
credits = [2, 3, 3, 4, 2, 4, 4, 3]
grade_points = [1, 2, 4, 2, 1, 2, 4, 2]
for i in range(len(credits)):
gpa_obj.addsubject(credits[i], grade_points[i])
# Test if the addsemester function is defined
with pytest.raises(ValueError):
obj.addsemester(gpa_obj)
assert len(obj.semester) == 0
def test_addsemester_multiple_semesters():
obj = Cgpa()
gpa_obj1 = Gpa()
gpa_obj2 = Gpa()
gpa_obj3 = Gpa()
# sem 1
credits = [2, 3, 3, 4, 2, 4, 4, 3]
grade_points = [8, 8, 7, 8, 10, 9, 9, 8]
for i in range(len(credits)):
gpa_obj1.addsubject(credits[i], grade_points[i])
# sem 2
credits = [2, 4, 3, 4, 2, 2, 4, 2]
grade_points = [7, 8, 7, 8, 10, 6, 9, 8]
for i in range(len(credits)):
gpa_obj2.addsubject(credits[i], grade_points[i])
# sem 3
credits = [2, 3, 4, 3, 2, 3, 4, 3]
grade_points = [10, 8, 7, 8, 7, 9, 7, 8]
for i in range(len(credits)):
gpa_obj3.addsubject(credits[i], grade_points[i])
# Test if the addsemester function is defined
obj.addsemester(gpa_obj1)
obj.addsemester(gpa_obj2)
obj.addsemester(gpa_obj3)
assert len(obj.semester) == 3
assert obj.semester[0].gpa == 8.36
assert obj.semester[1].gpa == 7.956521739130435
assert obj.semester[2].gpa == 7.875
# calc function test cases
def test_cgpa_calc():
obj = Cgpa()
gpa_obj = Gpa()
# add subjects
credits = [2, 3, 3, 4, 2, 4, 4, 3]
grade_points = [8, 8, 7, 8, 10, 9, 9, 8]
for i in range(len(credits)):
gpa_obj.addsubject(credits[i], grade_points[i])
obj.addsemester(gpa_obj)
# Test if the calc function is defined
obj.calc()
assert obj.cgpa == 8.36