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()
chepner
539k77 gold badges596 silver badges748 bronze badges
-
Don't use a property if the getter/setters just provide unrestricted or unmodified access to the underlying attribute; just use the attribute directly.chepner– chepner2019年12月16日 22:09:02 +00:00Commented Dec 16, 2019 at 22:09
-
Good point! I get stuck with start - stopdanH– danH2019年12月16日 22:28:04 +00:00Commented 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.James Li– James Li2019年12月16日 23:03:37 +00:00Commented Dec 16, 2019 at 23:03
1 Answer 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')
answered Dec 16, 2019 at 22:11
chepner
539k77 gold badges596 silver badges748 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
danH
Appreciated, and makes a lot of sense setting up, I get blocked in trying to perform a test on start / stop.
chepner
See update; it's just a matter of calling the method and checking what it returns.
danH
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 .....
Explore related questions
See similar questions with these tags.
lang-py