Skip to content

Commit

Permalink
Merge pull request #1 from thecoding-society/test
Browse files Browse the repository at this point in the history
Added testing using pytest
  • Loading branch information
aviiciii authored Feb 14, 2023
2 parents 4b4dc01 + 32d6e1b commit e2576f8
Show file tree
Hide file tree
Showing 3 changed files with 311 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

*.pyc
50 changes: 30 additions & 20 deletions program.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@

class subject:
"""
This class stores the credit and grade point of a subject.
"""

# constructor
def __init__(self, credit, grade_point):

# check if the credit and grade point are integers and are not negative
if type(credit) != int:
raise TypeError("Credit should be an integer.")
if type(grade_point) != int:
raise TypeError("Grade Point should be an integer.")
if credit < 0 or grade_point < 0:
raise ValueError("Credit and Grade Point cannot be negative.")


self.credit = credit
self.grade_point = grade_point

Expand Down Expand Up @@ -36,16 +46,14 @@ def addsubject(self, credit, grade_point):
This function adds a subject to the list of subjects.
"""

# Check if credit and grade point are valid
if credit < 0 or grade_point < 0:
print("Credit and Grade Point cannot be negative.")
return

# limits on credit and grade point

if credit > 8:
print("Credit cannot be greater than 8.")
return
raise ValueError("Credit cannot be greater than 8.")
if grade_point > 10:
print("Grade Point cannot be greater than 10.")
return
raise ValueError("Grade Point cannot be greater than 10.")


# Add subject to the list
self.sub.append(subject(credit, grade_point))
Expand All @@ -58,11 +66,9 @@ def calc(self):
This function calculates the GPA of the student.
"""

# Check if at least one subject is added
if self.no_of_subjects < 1:
print("No subjects added yet. Run addsubject(credit, grade_point) first.")
return

if self.no_of_subjects == 0:
raise ValueError("No subjects added yet. Run addsubject(credit, grade_point) first.")

# Iterate through the list of subjects and calculate the numerator and denominator
for i in range(self.no_of_subjects):
self.numerator += self.sub[i].credit * self.sub[i].grade_point
Expand All @@ -84,15 +90,18 @@ def display(self):
This function displays the number of subjects and the GPA.
"""


# Check if at least one subject is added
if self.no_of_subjects < 1:
print("No subjects added yet. Run addsubject(credit, grade_point) first and then run calc().")
return

if self.no_of_subjects == 0:
raise ValueError("No subjects added yet. Run addsubject(credit, grade_point) first.")




# Check if GPA is calculated
if self.gpa == 0.0:
print("No GPA calculated yet. Run calc() first.")
return
raise ValueError("No GPA calculated yet. Run calc() first.")


# Display the number of subjects and the GPA
print("No of subjects: ", self.no_of_subjects)
Expand All @@ -106,6 +115,7 @@ def __str__(self):
return "No of subjects: " + str(self.no_of_subjects) + "GPA: " + str(self.gpa)



def main():
# Create an object of the gpa class
gpa1 = gpa()
Expand Down
279 changes: 279 additions & 0 deletions test_program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
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, 7)
obj.addsubject(1, 9)
obj.addsubject(3, 8)
obj.addsubject(2, 3)
obj.calc()

assert obj.gpa == 7.9


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: 7.9\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

0 comments on commit e2576f8

Please sign in to comment.