[Tutor] Using unittest module for Test Driven Development

Alex Kleider akleider at sonic.net
Mon Aug 25 02:23:35 CEST 2014


Given
$ cat compare.py
#!/usr/bin/env python3
import os
import sys

def get_args():
     try:
         args = sys.argv[1:]
     except IndexError:
         return
     return [arg for arg in args if os.path.isfile(arg)]

if __name__ == "__main__":
     print(get_args())

How can one unittest get_args()?
It seems to me that 'unittest'ing only works to test functions and 
methods,
not programs.

Here's my attempt at a testing module:
$ cat testing.py
#!/usr/bin/env python3
import unittest
import compare_sizes

class CompareTesting(unittest.TestCase):

     def setUp(self):
         pass

     def test_nothing(self):
         self.assertEqual(not False, True)

     def test_get_file_param(self):
         self.assertTrue(compare_sizes.get_args() == ['file1', 'file2'])

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

Is there a way that I can provide the file name command line parameters
to compare.py so that its get_args function can be tested?

Thanks in advance for any advice.
AlexK

ps I'm on Ubuntu14.4, using Python3.4.0


More information about the Tutor mailing list