danH
danH

Reputation: 105

Unittesting Python class

class Car:
 def __init__(self):
     self.__engine_size = 2.0
     self.__colour = str
 @property # getter
 def engine_size(self):
     return self.__engine_size
 @property # getter
 def colour(self):
     return self.__colour
 @colour.setter
 def colour(self, value):
     self.__colour = value
 def start(self):
     return 'Engine started!!....ggggrrrrrr'
 def stop(self):
     return 'Engine stopped!!...'

Hey guys, trying to perform a test on this piece of code but can't think of ways. See bellow what I did and suggest other ways if known.

import unittest

from car import __init__

class TestCarMethods(unittest.TestCase):

    # case assertion no1
    '''

    '''

    def test_car_colour(self):

        # arrange
        __engine_size = 3.2
        __colour = 'red'

        # act
        result = ('red')

        # assert
        self.assertEqual(result, 'red')



       if __name__ == '__main__':
         unittest.main()

Upvotes: 0

Views: 191

Answers (1)

chepner
chepner

Reputation: 530922

You need to create and work with an instance of your class.

class TestCarMethods(unittest.TestCase):
    def setUp(self):
        self.car = Car()

    def test_car_color(self):
        self.assertEqual(self.car.color, 'red')  # If the default is, in fact, red

    def test_set_color(self):
        self.car.color = 'blue'

        self.assertEqual(self.car.color, 'blue')

    def test_start(self):
        self.assertEqual(self.car.start(), 'Engine started!!....ggggrrrrrr')

Upvotes: 1

Related Questions