【问题标题】:Testing a car class Unittests测试汽车类单元测试
【发布时间】:2021-03-31 23:18:28
【问题描述】:

我在运行测试时不断收到 2 个错误。测试结果:test_details(测试汽车细节是否正确)和test_price_reduction(测试降价记录是否正确)

提前感谢您的帮助。

我正在测试这段代码:

import unittest

from car import Car


class TestCar(unittest.TestCase):
    """ Tests for the Car class """

    grade = 0

    @classmethod
    def setUpClass(cls):
        """ Sets up the Test Case """
        TestCar.grade = 0
        print("Grading your Car class... ")

    @classmethod
    def tearDownClass(cls):
        """ Tears down the Test Case """
        print("Your grade for this part of the lab is: %d/8" % TestCar.grade)

    def setUp(self):
        """ Sets up the car objects to test in the subsequent tests """
        self.car1 = Car("Honda", "Civic", 2015, 4999.99, 9999.99)
        self.car2 = Car("Mazda", "Miata", 1990, 1500.0, 3999.88)

    def test_constructor(self):
        """ Tests that the car is successfully created """

        self.assertIsNotNone(self.car1)
        TestCar.grade += 1
        self.assertIsNotNone(self.car2)
        TestCar.grade += 1

    def test_details(self):
        """ Tests that the car details are correct """
        self.assertEqual("2015 Honda Civic for sale for $9999.99", self.car1.get_details())
        TestCar.grade += 1
        self.assertEqual("1990 Mazda Miata for sale for $3999.88", self.car2.get_details())
        TestCar.grade += 1

    def test_profit(self):
        """ Tests that the profit is calculated correctly """
        self.assertEqual(self.car1.calc_profit(), 5000.0)
        TestCar.grade += 1
        self.assertEqual(self.car2.calc_profit(), 2499.88)
        TestCar.grade += 1

    def test_price_reduction(self):
        """ Tests that the price reduction is recorded correctly """
        self.car1.reduce_price(1000.50)
        self.assertEqual(self.car1.calc_profit(), 3999.50)
        TestCar.grade += 1

        self.car2.reduce_price(999.88)
        self.assertEqual(self.car2.calc_profit(), 1500.0)
        TestCar.grade += 1


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

使用此代码:

class Car:
    """ Represents a car in a car lot """

    def __init__(self, make, model, year, cost, price):
        """ Initializes the car details """
        self._make = make
        self._model = model
        self._year = year
        self._cost = cost
        self._price = price

    def calc_profit(self):
        """ Returns the projected profit """
        return self._price - self._cost

    def get_details(self):
        print(f"{self._year} {self._make} {self._model} for sale for ${self._price}")

    def reduce_price(self, reduction):
        self._price = self._cost - reduction


car1 = Car('Honda', 'Civic', 2015, 5000.00, 9999.99)
car1.calc_profit()
car1.get_details()
car1.reduce_price(1000.50)
print(car1._price)

car2 = Car('Mazda', 'Miata', 1990, 2499.88, 3999.88)
car2.calc_profit()
car2.get_details()
car2.reduce_price(999.88)
print(car2._price)

def main():
    pass


if __name__ == '__main__':
    main()

这是我的输出:

2015 Honda Civic for sale for $9999.99
3999.5
1990 Mazda Miata for sale for $3999.88
1500.0

我在运行测试时不断收到 2 个错误。 测试结果:test_details(测试汽车细节是否正确) 和 test_price_reduction(测试降价记录是否正确)

提前感谢您的帮助。

【问题讨论】:

    标签: python python-3.x function unit-testing python-unittest


    【解决方案1】:

    您的get_details() 函数使用print 打印出一个字符串。它根本不返回任何值。如果函数中没有return 语句,则默认返回类型为None。这就是为什么当您self.assertEqual("2015 Honda Civic for sale for $9999.99", self.car1.get_details()) 时您未通过测试。 "2015 Honda Civic for sale for $9999.99" 不可能等于 None。您可以更新您的 get_details 函数以返回一个字符串而不是打印它。

    def get_details(self):
        return f"{self._year} {self._make} {self._model} for sale for ${self._price}"
    

    您必须记住将您的使用情况从 car1.get_details() 更新为 print(car1.get_details())

    您的第二个问题是您的reduce_price 函数将您的新价格设置为等于成本减去折扣。我假设您的意思是通过旧价格减去降价来降低价格。

    def reduce_price(self, reduction):
        self._price = self._price - reduction
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      相关资源
      最近更新 更多