File: testing-plugins.rst

package info (click to toggle)
trac 1.6-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,620 kB
  • sloc: python: 81,903; javascript: 2,219; makefile: 561; sh: 92; xml: 12
file content (69 lines) | stat: -rw-r--r-- 2,463 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Writing Tests for Plugins
=========================

Testing a VCS backend
---------------------

You'll need to make several subclasses to get this working in the
current test infrastructure.  But first, we start with some imports.
These are pretty much required for all plugin tests::

    from trac.tests.functional import (FunctionalTestSuite,
                                       FunctionalTestCaseSetup,
                                       FunctionalTwillTestCaseSetup, tc)
    from trac.tests.functional import testenv

Now subclass
:class:`~trac.tests.functional.testenv.FunctionalTestEnvironment`.
This allows you to override methods that you need to set up your repo
instead of the default Subversion one::

    class GitFunctionalTestEnvironment(testenv.FunctionalTestEnvironment):
        repotype = 'git'

        def create_repo(self):
            os.mkdir(self.repodir)
            self.call_in_repo(["git", "init"])
            self.call_in_repo(["git", "config", "user.name", "Test User"])
            self.call_in_repo(["git", "config", "user.email", "test@example.com"])

        def get_enabled_components(self):
            return ['tracext.git.*']

        def get_repourl(self):
            return self.repodir + '/.git'
        repourl = property(get_repourl)


Now you need a bit of glue that sets up a test suite specifically for
your plugin's repo type.  Any testcases within this test suite will
use the same environment.  No other changes are generally necessary on
the test suite::

    class GitFunctionalTestSuite(FunctionalTestSuite):
        env_class = GitFunctionalTestEnvironment

Your test cases can call functions on either the :ref:`tester
<functional-tester>` or :ref:`twill commands <using-twill>` to do
their job.  Here's one that just verifies we were able to sync the
repo without issue::

    class EmptyRepoTestCase(FunctionalTwillTestCaseSetup):
        def runTest(self):
            self._tester.go_to_timeline()
            tc.notfind('Unsupported version control system')

Lastly, there's some boilerplate needed for the end of your test file,
so it can be run from the command line::

    def suite():
        # Here you need to create an instance of your subclass
        suite = GitFunctionalTestSuite()
        suite.addTest(EmptyRepoTestCase())
        # ...
        suite.addTest(AnotherRepoTestCase())
        return suite

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