'I am having problem creating a unit test in python. can anyone let me know what's my mistake?

Someone help me out with this problem. I was able to run the cars.py file but for some reason I dont know how to properly make a unit test with a value that has been read from csv file.

CSV file content:

Car Name,Price
Toyota Camry,28000
Lexus,35000
Range Rover,75000
Volswagen Beetle,32000
Audi A5,55000
Jeep,52000
Toyota RAV4,48000
Nissan Ultima,29500
Aston Martin,85000

cars.py content

import csv
import math
def expensive_Car(expensiveCar):
    with open('Cars.csv', "r") as csvfile:
        reader = csv.reader(csvfile, delimiter = ",")
        header = next(reader)
        line_count = 0
        expensiveCarCompany= 0

        for row in reader:
            if line_count == 0:
                line_count+=1
            
            else:
                line_count+=1
                     
            #Finding the most expensive car
            if int(row[1]) > expensiveCar:
                expensiveCarCompany, expensiveCar = row[0], int(row[1]) 
           
    print(f'Expensive car company: {expensiveCarCompany} which is ${expensiveCar}.')    
    return expensiveCarCompany, expensiveCar 

def cheapest_Car(cheapestCar):
    with open('Cars.csv', "r") as csvfile:
        reader = csv.reader(csvfile, delimiter = ",")
        header = next(reader)
    
        line_count = 0
        cheapestCarCompany = 0
        for row in reader:
            if line_count == 0:
                line_count+=1
            
            else:
                line_count+=1
            
            #Finding the most cheapest car
            if int(row[1]) < cheapestCar:
                cheapestCarCompany, cheapestCar = row[0], int(row[1])
     
    print(f'Cheapest car company: {cheapestCarCompany} which is ${cheapestCar}.')
    return cheapestCarCompany, cheapestCar

if __name__ == "__main__":
    cheapCar = cheapest_Car(math.inf)
    priceyCar = expensive_Car(-math.inf)

##Unittest content##

import unittest
import cars
class test_carPrice(unittest.TestCase):
    
    def test_ExpensiveCar(self):
        result = cars.expensive_Car(self)
        self.assertEquals(result, 85000)
    
    def test_CheapestCar(self):
        self.assertIs(cars.cheapest_Car, 28000)

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

This is the first time learning python and I don't know what I did wrong but it keeps on showing me these errors.

##Error message from cars.py :##
'>' not supported between instances of 'int' and 'test_carPrice'

##error message from unittest##
<function cheapest_Car at 0x000002013B511240> is not 28000
in test_CheapestCar self.assertIs(cars.cheapest_Car, 28000)



    




Solution 1:[1]

Your methods don't need a parameter, as the method is the one finding the cheapest or more expensive car, it is not something you give to the method.

In you tests you should have called them the same, with math.inf, and call the method, because cars.cheapest_Car is the function itself, and you can't compare it to an int

Tests become

def test_ExpensiveCar(self):
    company, value = expensive_Car()
    self.assertEquals(value, 85000)

def test_CheapestCar(self):
    company, value = cheapest_Car()
    self.assertEquals(value, 28000)

And with better namings you have

def expensive_car():
    with open('Cars.csv', "r") as csvfile:
        reader = csv.reader(csvfile, delimiter=",")
        next(reader)
        expensive_car_company = 0
        expensive_car = -math.inf

        for row in reader:
            if int(row[1]) > expensive_car:
                expensive_car_company, expensive_car = row[0], int(row[1])

    print(f'Expensive car company: {expensive_car_company} which is ${expensive_car}.')
    return expensive_car_company, expensive_car

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 azro