简体   繁体   中英

Python unittest runs my non test file

I have two files, FinalProject.py and Test_FinalProject.py.

The code for my test class is:

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()

Every time I run this from within PyCharm or cmd, my FinalProject.py code runs instead, no matter what changes I make. Where am I going wrong? Thanks.

Edit: Code from 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)

let's try this:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM