📄 __init__.py
字号:
#!/usr/bin/python"""functional_testsWhile unittests work well for testing facets of an implementation, they fail toprovide assurances that the user-visible functions work in practice. Here, wecomplement the unittests with functional tests that drive the system as a userwould to verify user visible functionality. These functional tests are run aspart of the unittests.So, we use Twill to verify Trac's functionality as served by tracd (and in thefuture, other frontends).Unlike most unittests, we setup a single fixture against which we run all thetestcases. This is for two reasons: Primarily, that provides us with a morecomplex set of data to test against and thus more room for triggering bugs.Secondarily, the cost of setting up a new Trac environment and Subversionrepository is significant, so recreating the fixture for each test would bevery costly.There are two primary objects invovled in the testing, theFunctionalTestEnvironment and the FunctionalTester.FunctionalTestEnvironment represents the Trac environment, the Subversionrepository, and the server. The server will be run on a random local port inthe range 8000-8999. A subdirectory named 'tracenv' will be created containingthe Trac environment, Subversion repository, and the user authenticationinformation. An 'admin' user is created and given TRAC_ADMIN privs early inthe testing. There are other users added as well. All accounts are setup witha password equalling the username. The test environment is left behind afterthe testing has completed to assist in debugging.FunctionalTester provides code reuse for the testcases to allow a higher-leveldescription of the more complicated bugs. For example, creating a new ticketis the first step in regression testing many things, so FunctionalTesterprovides a create_ticket() method. That method is written as if it were itselfa testcase for creating a ticket, so there is a testcase that simply calls thatmethod, and other testcases that use it as a higher-level step don't have toworry about basic issues such as if the ticket was successfully created.Requirements: - Twill (http://twill.idyll.org/) - subprocess (py2.4)"""import osimport signalimport sysimport timeimport shutilimport statimport unittestimport exceptionsimport tracfrom trac.tests.functional.compat import close_fds, rmtree# Handle missing twill and/or subprocess so we can print a useful 'SKIP'# message. We import subprocess first to allow customizing it on Windows# to select pywin32 in favor of _subprocess for low-level calls. If Twill# is allowed to load first, its (unmodified) copy will always be loaded.try: import subprocessexcept ImportError: subprocess = Nonefrom better_twill import twill, b, tc, ConnectErrortry: # This is the first indicator of whether the subversion bindings are # correctly installed. from svn import coreexcept ImportError: core = Nonefrom datetime import datetime, timedeltafrom trac.tests.contentgen import random_sentence, random_page, random_word, \ random_unique_camelfrom trac.util.compat import sorted, reversedfrom trac.test import TestSetup, TestCaseSetupinternal_error = 'Trac detected an internal error:'trac_error = 'Trac Error'if twill and subprocess: trac_source_tree = os.path.normpath(os.path.join(trac.__file__, '..', '..')) # testing.log gets any unused output from subprocesses logfile = open(os.path.join(trac_source_tree, 'testing.log'), 'w') # functional-testing.log gets the twill output twill.set_output(open(os.path.join(trac_source_tree, 'functional-testing.log'), 'w')) from trac.tests.functional.testenv import FunctionalTestEnvironment from trac.tests.functional.tester import FunctionalTester class FunctionalTestSuite(TestSetup): """TestSuite that provides a test fixture containing a FunctionalTestEnvironment and a FunctionalTester. """ def setUp(self, port=None): """If no port is specified, use a semi-random port and subdirectory 'testenv'; but if a port is specified, use that port and subdirectory 'testenv<portnum>'. """ if port == None: port = 8000 + os.getpid() % 1000 dirname = "testenv" else: dirname = "testenv%s" % port dirname = os.path.join(trac_source_tree, dirname) baseurl = "http://localhost:%s" % port self._testenv = FunctionalTestEnvironment(dirname, port, baseurl) self._testenv.start() self._tester = FunctionalTester(baseurl, self._testenv.repo_url()) self.fixture = (self._testenv, self._tester) def tearDown(self): self._testenv.stop() class FunctionalTestCaseSetup(TestCaseSetup): """Convenience class to expand the fixture into the _testenv and _tester attributes.""" def setUp(self): self._testenv, self._tester = self.fixture class FunctionalTwillTestCaseSetup(FunctionalTestCaseSetup): failureException = twill.errors.TwillAssertionErrorelse: # We're going to have to skip the functional tests class FunctionalTwillTestCaseSetup: pass class FunctionalTestCaseSetup: pass# Twill's find command accepts regexes; some convenient but complex regexes# & regex factories are provided here (only one so far):def regex_owned_by(username): return '(Owned by:(<[^>]*>|\\n| )*%s)' % usernamedef suite(): if twill and subprocess and core: from trac.tests.functional.testcases import suite suite = suite() else: diagnostic = "SKIP: functional tests" if not twill: diagnostic += " (no twill installed)" if not subprocess: diagnostic += " (no subprocess installed)" if not core: diagnostic += " (no Subversion bindings installed)" print diagnostic # No tests to run, provide an empty suite. suite = unittest.TestSuite() return suiteif __name__ == '__main__': unittest.main(defaultTest='suite')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -