如何使用assertEqual验证csv中加载的行?

2024-03-29 06:47:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在unit_test.py中进行单元测试,以验证csv文件中的所有行是否已成功加载到main.py中。main.py中的代码工作得非常好,当我在main.py中使用“print(len(my_file)”进行测试时,它确实显示加载了40000行在unit_test.py中,它显示只有17行。有人能给我指出正确的方向吗?我想我使用assertEqual的方式是错误的

Main.py

import csv

datafile = './AB_LA_2019.csv'

class AirbnbData:

........
........
    def load_data(self):
    with open(datafile, 'r', newline='') as file:
        csvreader = csv.reader(file)
        next(file)
        my_file = [(row[1], row[2], int(row[3])) for row in csvreader]
        self._airbnbdata = my_file


    self._generate()

单位检验

   import unittest
   import main as airbnb


   class testAirbnb(unittest.TestCase):

       def test_line(self):
       self.assertEqual(40000, len(airbnb.datafile))