0
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()
3
  • Don't use a property if the getter/setters just provide unrestricted or unmodified access to the underlying attribute; just use the attribute directly. Commented Dec 16, 2019 at 22:09
  • Good point! I get stuck with start - stop Commented Dec 16, 2019 at 22:28
  • tests come out naturally by defining what should it do , but not true when you try saying how does it do. for your piece of code . valid test would be the return values of start/stop, exception checking can be addwed too. Commented Dec 16, 2019 at 23:03

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

3 Comments

Appreciated, and makes a lot of sense setting up, I get blocked in trying to perform a test on start / stop.
See update; it's just a matter of calling the method and checking what it returns.
Gosh, I think I need a break :))) thanks man. for a weird reason I was complicating it thinking that the functions start/ stop is called after color is set .....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.