Difference between revisions of "Writing Python Tests"

(Add documentation on writing python tests.)
 
(Add good patterns to Writing Python Tests for Samba)
Line 58: Line 58:
  
 
descend from this class, so the self.tempdir attribute is available with a temporary directory.  The framework will assert that directory is empty when the test is done.
 
descend from this class, so the self.tempdir attribute is available with a temporary directory.  The framework will assert that directory is empty when the test is done.
 +
 +
=Good Patterns=
 +
 +
==Where to add new tests==
 +
 +
Samba Tests are scattered around the code.  However, in general, unless already related to or derived from other tests, new tests should be added in
 +
 +
samba/python/tests
 +
 +
==use subunitrun==
 +
 +
Tests should be run using subunitrun if possible, declared in tests.py via planpythontestsuite(), not planoldpythontestsuite()
 +
 +
==Avoid innovation==
 +
 +
Try not to make your new test special.  The more our tests look like each other, the easier it is for new developers to follow the same patterns.

Revision as of 20:19, 1 August 2017

Writing Python Tests for Samba

unittest.TestCase

Python unittest.TestCase is the standard basis on which all of Samba's python based tests should be written.

This provides helpful routines such as assertTrue() etc, which provide a cosnsitent pattern in the tests.

samba.tests.TestCase

Samba's subclass samba.tests.TestCase, provides additional helper routines such as env_loadparm()

pydoc

You can explore the methods provided using pydoc:

PYTHONPATH=bin/python pydoc samba.tests.TestCase

Testing Samba binaries

When testing Samba binaries, please use:

samba.tests.BlackboxTestCase

Testing samba-tool

samba-tool is a special case, as it is written in python. To avoid a fork()/exec() of python from python, use

samba.tests.samba_tool.base.SambaToolCmdTest

The runcmd() and runsubcmd() methods allows testing of a samba-tool command in the same process, which also allows the use of

samba.tests.TestCase.get_creds_ccache_name()

This example from source4/torture/drs/python/fsmo.py shows how to run an authenticated subcommand without forcing a new kinit:

ccache_name = self.get_creds_ccache_name()
cmd_line_auth = "--krb5-ccache=%s" % ccache_name
(result, out, err) = self.runsubcmd("fsmo", "transfer",
                                    "--role=%s" % role,
                                    "-H", "ldap://%s:389" % DC,
                                    cmd_line_auth)


Tempoary directories

If you need a temp dir, use

samba.tests.TestCaseInTempDir

Both

samba.tests.samba_tool.base.SambaToolCmdTest

and

samba.tests.BlackboxTestCase

descend from this class, so the self.tempdir attribute is available with a temporary directory. The framework will assert that directory is empty when the test is done.

Good Patterns

Where to add new tests

Samba Tests are scattered around the code. However, in general, unless already related to or derived from other tests, new tests should be added in

samba/python/tests

use subunitrun

Tests should be run using subunitrun if possible, declared in tests.py via planpythontestsuite(), not planoldpythontestsuite()

Avoid innovation

Try not to make your new test special. The more our tests look like each other, the easier it is for new developers to follow the same patterns.