Unit tests

A great place to look for examples of how to use the Splunk® SDK for Python is in the unit tests. These are the same tests that we used to validate the core SDK library.

The test suite uses Python's standard library and the built-in unittest library.

Note: If you're using Python 2.7, you're all set. However, if you are using Python 2.6, you'll also need to install the unittest2 library to get the additional features that were added to Python 2.7 (just run pip install unittest2 or easy_install unittest2).

To run the unit tests, you'll first need to install several test apps in Splunk Enterprise:

  1. Open a command prompt in the splunk-sdk-python directory.

  2. Enter the following, and then press Enter:

    python setup.py build dist

    This command creates a new build directory in the splunk-sdk-python directory with three test Splunk apps inside: github_forks.spl, random_numbers.spl, and searchcommands_app.spl.

  3. Install each app into your Splunk server. For instructions, see the "If you are not connected to the internet" section of "Where to get more apps and add-ons".

After you've installed the three apps, you're ready to run the tests. Open a command prompt in the splunk-sdk-python directory and enter the following:

python setup.py test
Note: You can safely ignore "Test requires sdk-app-collection. Skipping." messages.

You can also run individual test files, which are located in /splunk-sdk-python/tests. For example, to run the apps test, open a command prompt in the /splunk-sdk-python/tests subdirectory and enter:

python test_app.py

Code coverage

Coverage.py is an excellent tool for measuring code coverage of Python programs.

To install it, use Easy Install:

easy_install coverage

Or use pip:

pip install coverage

To generate a report of the code coverage of the unit test suite, open a command prompt in the /splunk-sdk-python directory and enter:

python setup.py coverage

This command runs the entire test suite and writes an HTML coverage report to the /splunk-sdk-python/coverage_report directory.

For more information about Coverage.py, see the author's website (http://nedbatchelder.com/code/coverage/).