0

I'm fairly new to unit testing in general and I'm trying to generate unit tests for a script I'm developing in Python. The script is designed to take in a data file and several command line arguments, and then run several functions in order while updating a database. For example, here is how my script would run:

import argparse
if __name__ == "__main__": 
    parser = argparse.ArgumentParser(description='Script to process data files')
    parser.add_argument('-f', help='The absolute file_path to the source file')
    args = parser.parse_args()
    main(args)

def main(args):
    file_info = get_file_info(args)
    process1(args, file_info)
    process2(args, file_info)

def file_info(args):
    file_info = {'path':args.file_path...}
    ...
    return file_info

def process1(args):
    #update database1
    ...

def process2(args):
    #update database2
    ...

As you can see process1 and process2 are meant to modify the database, not to return a value, so the only way to check if those worked properly is to check the database to see if it has the correct values. I can do this easily in a unit test, but I want to run similar unit tests on separate calls to the main function. So I want my unit tests to look like this:

import unittest
import myScript

class Test_Load(unittest.TestCase):        
    test_args_1 = ['-f','C:\file1.txt']
    myScript.main(test_args_1)

    def test_get_file_info_1(self):
        test_file_info = {'foo':'bar'}
        self.assertDictEqual(test_file_info, myScript.get_file_info(test_args_1))

    def test_process1_1(self):
        #get list from sql query on database
        test_db_info = {'load_time':'1997-07-16T19:20:30', 'file_size':512}
        self.assertDictEqual(test_db_info, sql_query)

    def test_process2_1(self):
        #get list from sql query on database
        test_db_info = {'foo':'bar'}
        self.assertDictEqual(test_db_info, sql_query)

    test_args_2 = ['-f','C:\file2.txt']
    myScript.main(test_args_2)

    def test_get_file_info_2(self):
        test_file_info = {'foo':'bar'}
        self.assertDictEqual(test_file_info, myScript.get_file_info(test_args_2))

    def test_process1_2(self):
        #get list from sql query on database
        test_db_info = {'load_time':'2017-02-08T12:00:00', 'file_size':1024}
        self.assertDictEqual(test_db_info, sql_query)

    def test_process2_2(self):
        #get list from sql query on database
        test_db_info = {'foo':'foobar'}
        self.assertDictEqual(test_db_info, sql_query)
    ...

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

The get_file_info() function returns a dictionary so I should have no problem testing that.

So I want to call the main of my script and then run some tests on the expected results. Am I able to call my scripts main like this in the Test_Load class? And is there a better way to group these unit tests so they can be run independently of each other?

dselgo
  • 271
  • 3
  • 14
  • 1
    Seems like you want an [integration test](https://en.wikipedia.org/wiki/Integration_testing), not a unit test. – erip Feb 08 '17 at 16:54
  • you have setUp/teardown metod, setUpClass/tearDownClass, please spend some hours to reading the docu about – Ari Gold Feb 08 '17 at 17:39
  • Thanks for the input! I was not aware of setUp/tearDown methods so those helped a lot. After reading up on integration testing I realized that was what I was trying to do, but after examining how I wanted to test my script I decided that I wanted to create true unit tests. I re-ordered my tests so that they would instead test each method instead of running the entire script on a series of files. – dselgo Mar 07 '17 at 17:28

0 Answers0