Changes between Version 20 and Version 21 of TracDev/UnitTests
- Timestamp:
- Jan 8, 2016, 8:32:33 AM (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TracDev/UnitTests
v20 v21 1 = Trac Unit Tests = 1 [[PageOutline(2-5,Contents,pullout)]] 2 2 3 Many of the Trac Python modules are accompanied by unit tests. You should run the tests whenever making changes, to be confident you haven't broken anything. ''Note though that the coverage of application code by the unit tests is still rather poor, and even if it were better, not having broken the unit tests does not mean you haven't broken the application! Unit tests do not replace manual testing.'' 3 = Trac Unit Tests 4 4 5 Ideally, you'd also include new unit tests for a change or enhancement, even if you're just submitting a patch. Patches that break the unit tests are a lot less likely to get integrated than patches that add unit tests for the new or changed functionality.5 Many of the Trac Python modules are accompanied by unit tests. You should run the tests whenever making changes, to be confident you haven't broken anything. Note though that the coverage of application code by the unit tests is still rather poor, and even if it were better, not having broken the unit tests does not mean you haven't broken the application! Unit tests do not replace manual testing. 6 6 7 == Running the tests == 7 Ideally, also include new unit tests for a change or enhancement, even if you are just submitting a patch. Patches that break the unit tests are a lot less likely to get integrated than patches that add unit tests for the new or changed functionality. 8 9 == Running the tests 8 10 9 11 You can run the unit tests from the command line by executing: 10 {{{ 12 {{{#!sh 11 13 python -m trac.test --skip-functional-tests 12 14 }}} 13 or unix way: 14 {{{ 15 16 or the Unix way: 17 {{{#!sh 15 18 $ PYTHONPATH=. python trac/test.py --skip-functional-tests 16 19 }}} 20 17 21 or: 18 {{{ 22 {{{#!sh 19 23 $ make unit-test 20 24 }}} … … 25 29 26 30 This will run all the unit tests, but you can also run only those tests for a specific package or module. For example, to run the unit tests for the {{{trac.versioncontrol}}} package, execute: 27 {{{ 31 {{{#!sh 28 32 python -m trac.versioncontrol.tests.__init__ 29 33 }}} 34 30 35 or if you prefer bash: 31 {{{ 32 36 {{{#!sh 37 $ PYTHONPATH=. trac/versioncontrol/tests/__init__.py 33 38 }}} 34 39 35 40 To run the unit tests for the {{{trac.versioncontrol.cache}}} module, execute: 36 {{{ 41 {{{#!sh 37 42 python -m trac.versioncontrol.tests.cache 38 43 }}} 39 or 40 {{{ 41 $ PYTHONPATH=. trac/versioncontrol/tests/cache.py 44 45 or: 46 {{{#!sh 47 $ PYTHONPATH=. trac/versioncontrol/tests/cache.py 42 48 }}} 43 49 44 50 If you've made larger changes, then before running the tests, please make sure you've cleaned all {{{.pyc}}} files that may be left after removed or renamed source {{{*.py}}} files: 45 {{{ 46 51 {{{#!sh 52 $ find . -name *.pyc | xargs rm 47 53 }}} 48 54 49 === Test Database === 50 If you're developing on database different from SQLite, you may want to specify its URI using {{{TRAC_TEST_DB_URI}}} environment variable. 55 === Test Database 51 56 57 If you are developing on a database different from SQLite, you may want to specify its URI using the {{{TRAC_TEST_DB_URI}}} environment variable. 52 58 53 == Automatic builds ==59 == Automatic builds 54 60 55 61 The Trac unit tests are also run by the AutomaticBuilds. 56 62 57 == Adding tests ==63 == Adding tests 58 64 59 65 If you're adding a new module, or you want to add tests for a module that doesn't have any unit tests yet, you'll need to create a new Python module for the unit tests. … … 63 69 Under WritingUnitTests you find a tutorial for writing your own unit tests. 64 70 65 == Utility code for unit tests ==71 == Utility code for unit tests 66 72 67 73 The module [source:/trunk/trac/test.py#latest trac.test] contains a couple of functions and classes that can help writing unit tests. In particular, it provides an {{{InMemoryDatabase}}} class that can be used to test functionality that requires database access, without having to create an actual database on disk. Also there's a very simple factory for [http://c2.com/cgi/wiki?MockObject mock objects], which you can use to create quick substitutes of the "real" objects for testing. 68 74 69 == Optional Dependencies == 75 == Optional Dependencies 76 70 77 Some unit-tests depend on: 71 78 * [http://pytz.sourceforge.net/ pytz] … … 76 83 77 84 These can be installed with easy_install: 78 {{{ 85 {{{#!sh 79 86 easy_install pytz 80 87 easy_install Pygments … … 84 91 If these dependencies are not present, certain tests will be skipped. 85 92 86 == Troubleshooting == 93 == Troubleshooting 94 87 95 For general advice about Trac debugging, see TracTroubleshooting. 88 96 89 === !ImportError: no module named tests === 90 If you try to run the tests and you receive 97 === !ImportError: no module named tests 98 99 If you try to run the tests and you receive the following message: 91 100 {{{ 92 101 ImportError: No module named tests