Python unittest运行我的非测试文件

问题描述 投票:0回答:1

我有两个文件,FinalProject.py和Test_FinalProject.py。

我的测试类的代码是:

import unittest
import FinalProject

class TestFinalProject(unittest.TestCase):

    def test_read_dataset(self):
        dsm = FinalProject.DatasetManager()
        dsm.read_dataset()
        self.assertEqual(len(dsm.list_potatoes), 6480)

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

每次我从PyCharm或cmd中运行它时,无论我做了什么更改,我的FinalProject.py代码都会运行。我哪里错了?谢谢。

编辑:来自FinalProject.py的代码

class DatasetManager():

    def __init__(self):
        self.list_potatoes = []
        self.sorted_list_potatoes = []

    def read_dataset(self):
        try:
            with open('00010014-eng.csv') as csvfile:
                reader = csv.reader(csvfile, delimiter='|')
                self.list_potatoes = list(reader)
            csvfile.close()
        except IOError:
            print("Could not read file.")

    def get_record_count(self):
        return len(self.list_potatoes)
python unit-testing python-unittest
1个回答
0
投票

让我们试试这个:

class DatasetManager():

    def __init__(self):
        self.list_potatoes = []
        self.sorted_list_potatoes = []

    def read_dataset(self):
        try:
            with open('00010014-eng.csv') as csvfile:
                reader = csv.reader(csvfile, delimiter='|')
                self.list_potatoes += list(reader)
        except IOError:
            print("Could not read file.")

    def get_record_count(self):
        return len(self.list_potatoes)
© www.soinside.com 2019 - 2024. All rights reserved.