TeamCity Messages
This package integrates Python with the TeamCity Continuous Integration (CI) server, enabling the reporting of test results and other build information via TeamCity service messages. It provides integrations for popular testing frameworks like `unittest`, `pytest`, `nose`, `behave`, `twisted trial`, and code quality tools such as `flake8` and `pylint`. The current version is 1.33, with a release cadence that supports new Python versions and tool compatibility.
Warnings
- breaking Older Python versions are no longer supported. Version 1.28 dropped support for Python 2.6 and 3.5. Version 1.26 dropped support for Python 3.4.
- gotcha Compatibility issues can arise with newer versions of integrated testing and analysis tools (e.g., `coverage`, `flake8`, `pylint`, `pytest`) if `teamcity-messages` is not updated. Ensure your `teamcity-messages` version is compatible with your test ecosystem.
- gotcha For automatic test reporting with `nose`, `pytest`, and `flake8`, ensure the `TEAMCITY_VERSION` environment variable is set in your TeamCity build configuration. Without it, `teamcity-messages` might not auto-detect the TeamCity environment and report results.
- gotcha For `unittest` integration, direct modification of the test runner in your test script (as shown in the quickstart) is generally required. Unlike some other frameworks, `unittest` doesn't automatically hook into `teamcity-messages` without explicit code.
- gotcha TeamCity service messages are processed only when written to standard output (stdout). If your Python code or test runner redirects stdout or writes service messages to a file, TeamCity will not be able to parse them.
Install
-
pip install teamcity-messages
Imports
- TeamcityTestRunner
from teamcity.unittestpy import TeamcityTestRunner
- is_running_under_teamcity
from teamcity import is_running_under_teamcity
- TeamcityServiceMessages
from teamcity.messages import TeamcityServiceMessages
Quickstart
import unittest
from teamcity import is_running_under_teamcity
from teamcity.unittestpy import TeamcityTestRunner
import os
class MyTests(unittest.TestCase):
def test_success(self):
self.assertTrue(True, "This test should pass")
def test_failure(self):
self.assertEqual(1, 2, "This test should fail")
def test_skipped(self):
if os.environ.get('SKIP_TESTS') == '1':
self.skipTest("Skipping this test due to environment variable")
self.assertTrue(True)
if __name__ == '__main__':
print('Running tests...')
if is_running_under_teamcity():
print('Running under TeamCity, using TeamcityTestRunner')
runner = TeamcityTestRunner()
else:
print('Not running under TeamCity, using default TextTestRunner')
runner = unittest.TextTestRunner()
unittest.main(testRunner=runner, exit=False)