Edgewall Software

Changes between Version 3 and Version 4 of TracDev/WritingUnitTests


Ignore:
Timestamp:
Sep 25, 2014, 4:45:43 PM (10 years ago)
Author:
Franz Mayer <franz.mayer@…>
Comment:

Added Paragraph "Using several test methods"

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/WritingUnitTests

    v3 v4  
    33= Writing unit tests
    44
    5 This wiki page should show how to write your own unit tests for Trac or Trac plugin.
     5This wiki page shows how to write your own unit tests for Trac or Trac plugin.
    66
    77== Start writing `TestCase`s
     
    4141
    4242    def tearDown(self):
     43        self.env.shutdown()
    4344        shutil.rmtree(self.env.path)
    4445
     
    5859    unittest.main(defaultTest='suite')
    5960}}}
     61
     62== Using several test methods
     63
     64You can use several test methods within a `TestCase` class; they need to start with `test`. When you use method `setUp` it will be executed for each test method. If you only want to "set up" your test environment once of your test case, you need to use class method `setUpClass`.
     65
     66For example:
     67{{{#!py
     68# ... imports and such ...
     69class TestApi(unittest.TestCase):
     70
     71    @classmethod
     72    def setUpClass(self, port=None):
     73        super(TestApi, self).setUpClass()
     74        self.env = EnvironmentStub(enable=[
     75            'trac.*', 'logwatcher.api'
     76        ])
     77        # ... more lines ...
     78
     79    def test_get_logfile_name(self):
     80        # ... more lines ...
     81
     82    def test_get_log_settings_default(self):
     83        # ... more lines ...
     84
     85    @classmethod
     86    def tearDownClass(self):
     87        super(TestApi, self).tearDownClass()
     88        # you should also shutdown environment before deleting directory,
     89        # since it closes log files, shutdowns database connection and such
     90        self.env.shutdown()
     91        shutil.rmtree(dirPath)
     92}}}