Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

Unittest python csv file open test returns empty list

I have a function I'm trying to test that processes a csv file. One of the tests I'm trying to create checks that the expected and actual data match. I'm expecting the read_data to be output in the format of a single list with a dictionary. But the output is an empty list. How can I get unittest to return the correct data from the function, or am I using unittest incorrectly?

Function process_csv

def process_csv(file):
        '''
        Read csv file and return list of dictionaries
        ''' 
        return_list = []
        issue = 1
        summary = 2
        description = 3
        commit = 4
    
        print('Processing csv...',flush=True)
        with open(file) as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=',')
            line_count = 0
            for row in csv_reader:
                return_list.append({'issue': row[issue], 
                                    'summary': row[summary],
                                    'description': row[description], 'commit': row[commit]})
    
                line_count += 1
            print('Processed', line_count,'lines.')
    
        return return_list

Test test_process_csv_expected_output

@patch('myScript.open', new_callable=mock_open, create=True,read_data='issue, summary, description, commit')     
    def test_process_csv_expected_output(self,mock_file,mock_get_file_hash,mock_get_file_paths):
        '''
        Test process_csv expected output matches actual_output
        '''
        test_csv_path = str(TEST_CSV_PATH)
        test_file_path = str(STATIC_REPO)
        expected_data = {'issue': 'myIssue', 
                         'summary': 'test_page.page',
                         'description': 'Test description' , 'commit': 'ABCD'}
        mock_get_file_paths.return_value = test_file_path
        mock_get_file_hash.return_value = 'ABCD'

        myScript.process_csv(test_file_path)
        actual_output = git_build_opwg_branch.process_csv(test_file_path)
        self.assertEqual(expected_data,actual_output)

Error

FAIL: test_process_csv_expected_output (__main__.TestGitBuildOpwgBranch)

Test process_csv expected output matches actual_output
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:UserszjustAppDataLocalProgramsPythonPython38-32libunittestmock.py", line 1325, in patched
    return func(*newargs, **newkeywargs)
  File "test_git_build_opwg_branch.py", line 224, in test_process_csv_expected_output
    self.assertEqual(expected_data,actual_output)
AssertionError: {'issue': 'L9R-4604', 'summary': 'test_pa[153 chars]BCD'} != []

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Make sure to declare return_list = [] in the body of your function, or it won't exist in the context of the main part of your function.

def process_csv(file):
        '''
        Read csv file and return list of dictionaries
        '''
        
        return_list = []
        
        issue = 1
        summary = 2
        description = 3
        commit = 4
    
        print('Processing csv...',flush=True)
        with open(file) as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=',')
            line_count = 0
            for row in csv_reader:
                return_list.append({'issue': row[issue], 
                                    'summary': row[summary],
                                    'description': row[description], commit': row[commit]})
    
                line_count += 1
            print('Processed', line_count,'lines.')
    
        return return_list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...