Sunday, August 9, 2020

Code from Testing Video

Here is the customer class code:

class Customer():
    #constructor
    def __init__(self, id, name, email, rewards):
        self.id=id
        self.name=name
        self.email=email
        self.rewards=rewards
    
    #gets for fields
    def getId(self):
        return self.id
    def getName(self):
        return self.name
    def getEmail(self):
        return self.email
    def getRewards(self):
        return self.rewards
    
    #methods to add or use rewards
    def addReward(self, points):
        #make sure points is an integer
        points=int(points)
        self.rewards += points
    def useRewards(self, points):
        points=int(points)
        self.rewards = self.rewards - points
    

    # class string method
    def __str__(self):
        return self.name + ' ' + self.email

Here is the code for the test.py:

import unittest
from customer import Customer

class TestCustomer(unittest.TestCase):
    #set up an instance of the class
    def setUp(self):
        self.c=Customer(123,'Bret Brown', 'bb@gmail.com', 100)
    #test the get methods
    def test_Id(self):
        self.assertEqual(self.c.getId(), 123)
    def test_name(self):
        self.assertEqual(self.c.getName(),'Bret Brown' )
    # test the rewards
    def test_AddReward(self):
        self.c.addReward(50)
        self.assertEqual(self.c.getRewards(), 150)
    def test_UseReward(self):
        self.c.useRewards(50)
        self.assertEqual(self.c.getRewards(), 50)
    #other tests
    def test_Addreward2(self):
        self.c.addReward(50)
        self.assertGreater(self.c.getRewards(), 100)
    def test_addRewards3(self):
        self.c.addReward(50.5)
        self.assertEqual(self.c.getRewards(), 150)
    # test class string method
    def test_ToString(self):
        self.assertEqual(str(self.c), 'Bret Brown bb@gmail.com')

    

No comments:

Post a Comment