0

I have a python project with the following structure

test/ # unit tests to be run with nosetest
bin/  # utilities written in python to be installed
packagename/ # code for the package

For instance, bin/python_command might contain:

@cmdline.command(usage='subcommand1 description')
def cmd_mysubcommand1(opts, args):
    '''Code for python_command mysubcommand1'''                                                                                                   



if __name__ == '__main__':
    cmdline.main('Description of python_command')

Where cmdline is simply a library to help with the boilerplate code of parsing arguments and subcommand.

The question is, what's considered the best practice to unittest all the subcommands of bin/python_command (cmd_mysubcommand1 in particular) by including a test in the test directory? (i.e. they should be integrated in my current unittest suite).

Ideally, I would like to provide the textual command line of each command instead of specifically testing the final subcommand functions (to check that parsing of subcommands is working properly).

I have just read about scripttest but I don't know if it integrates well with the unittest python library and whether it supports mocking some of the objects used in the subcommands (which is a must for me).

fons
  • 4,905
  • 4
  • 29
  • 49
  • You want to unittest functions or you want to test commands (shell)? If you want to test functions, you test it like regular python things with mocking and so, I do not know why this time should be different. If you want to test a script (and it's command) it is more like integration test rather then unit test. – spinus Jul 20 '13 at 17:45
  • For one, it's different because the modules don't end with py and are not importable in a normal way (I guess there must be a way around this) but ideally I would like to test the commands with mocking. – fons Jul 20 '13 at 17:48
  • So why you just split this code to two modules? First for python-importable module and second (the proper script with the proper name) which just imports the first and run the commands. If you look at any pip installable program with a command line interface you look that is a common practice that in virtualenv in bin there is a script which just import a function from lib/site-packages from python module. – spinus Jul 20 '13 at 18:28
  • Thanks for the suggestions, I just ended up implementing a normal unittest using softlinks to the files not ending in .py as suggested [here](http://stackoverflow.com/questions/2601047/import-a-python-module-without-the-py-extension) – fons Jul 24 '13 at 21:34

0 Answers0