plugin.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. import os
  5. import platform
  6. try:
  7. import _pytest._pluggy as pluggy
  8. except ImportError:
  9. import pluggy
  10. import pytest
  11. import py
  12. from pytest_metadata.ci import (
  13. appveyor, circleci, gitlab_ci, jenkins, taskcluster, travis_ci)
  14. CONTINUOUS_INTEGRATION = {
  15. 'AppVeyor': ['APPVEYOR', appveyor.ENVIRONMENT_VARIABLES],
  16. 'CircleCI': ['CIRCLECI', circleci.ENVIRONMENT_VARIABLES],
  17. 'GitLab CI': ['GITLAB_CI', gitlab_ci.ENVIRONMENT_VARIABLES],
  18. 'Jenkins': ['JENKINS_URL', jenkins.ENVIRONMENT_VARIABLES],
  19. 'TaskCluster': ['TASK_ID', taskcluster.ENVIRONMENT_VARIABLES],
  20. 'Travis CI': ['TRAVIS', travis_ci.ENVIRONMENT_VARIABLES]}
  21. def pytest_addhooks(pluginmanager):
  22. from . import hooks
  23. pluginmanager.add_hookspecs(hooks)
  24. @pytest.fixture(scope='session')
  25. def metadata(pytestconfig):
  26. """Provide test session metadata"""
  27. return pytestconfig._metadata
  28. def pytest_addoption(parser):
  29. parser.addoption('--metadata',
  30. action='append',
  31. default=[],
  32. dest='metadata',
  33. metavar=('key', 'value'),
  34. nargs=2,
  35. help='additional metadata.')
  36. @pytest.hookimpl(tryfirst=True)
  37. def pytest_configure(config):
  38. config._metadata = {
  39. 'Python': platform.python_version(),
  40. 'Platform': platform.platform(),
  41. 'Packages': {
  42. 'pytest': pytest.__version__,
  43. 'py': py.__version__,
  44. 'pluggy': pluggy.__version__}}
  45. config._metadata.update({
  46. k: v for k, v in config.getoption('metadata')})
  47. plugins = dict()
  48. for plugin, dist in config.pluginmanager.list_plugin_distinfo():
  49. name, version = dist.project_name, dist.version
  50. if name.startswith('pytest-'):
  51. name = name[7:]
  52. plugins[name] = version
  53. config._metadata['Plugins'] = plugins
  54. for key, value in CONTINUOUS_INTEGRATION.items():
  55. [config._metadata.update({v: os.environ.get(v)})
  56. for v in value[1] if os.environ.get(v)]
  57. if hasattr(config, 'slaveoutput'):
  58. config.slaveoutput['metadata'] = config._metadata
  59. config.hook.pytest_metadata(metadata=config._metadata)
  60. def pytest_report_header(config):
  61. if config.getoption('verbose') > 0:
  62. return 'metadata: {0}'.format(config._metadata)
  63. @pytest.mark.optionalhook
  64. def pytest_testnodedown(node):
  65. # note that any metadata from remote slaves will be replaced with the
  66. # environment from the final slave to quit
  67. if hasattr(node, 'slaveoutput'):
  68. node.config._metadata.update(node.slaveoutput['metadata'])