brain_pytest.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (c) 2014-2016, 2018, 2020 Claudiu Popa <pcmanticore@gmail.com>
  2. # Copyright (c) 2014 Jeff Quast <contact@jeffquast.com>
  3. # Copyright (c) 2014 Google, Inc.
  4. # Copyright (c) 2016 Florian Bruhin <me@the-compiler.org>
  5. # Copyright (c) 2016 Ceridwen <ceridwenv@gmail.com>
  6. # Copyright (c) 2020-2021 hippo91 <guillaume.peillex@gmail.com>
  7. # Copyright (c) 2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  8. # Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  9. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  10. # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
  11. """Astroid hooks for pytest."""
  12. from astroid.brain.helpers import register_module_extender
  13. from astroid.builder import AstroidBuilder
  14. from astroid.manager import AstroidManager
  15. def pytest_transform():
  16. return AstroidBuilder(AstroidManager()).string_build(
  17. """
  18. try:
  19. import _pytest.mark
  20. import _pytest.recwarn
  21. import _pytest.runner
  22. import _pytest.python
  23. import _pytest.skipping
  24. import _pytest.assertion
  25. except ImportError:
  26. pass
  27. else:
  28. deprecated_call = _pytest.recwarn.deprecated_call
  29. warns = _pytest.recwarn.warns
  30. exit = _pytest.runner.exit
  31. fail = _pytest.runner.fail
  32. skip = _pytest.runner.skip
  33. importorskip = _pytest.runner.importorskip
  34. xfail = _pytest.skipping.xfail
  35. mark = _pytest.mark.MarkGenerator()
  36. raises = _pytest.python.raises
  37. # New in pytest 3.0
  38. try:
  39. approx = _pytest.python.approx
  40. register_assert_rewrite = _pytest.assertion.register_assert_rewrite
  41. except AttributeError:
  42. pass
  43. # Moved in pytest 3.0
  44. try:
  45. import _pytest.freeze_support
  46. freeze_includes = _pytest.freeze_support.freeze_includes
  47. except ImportError:
  48. try:
  49. import _pytest.genscript
  50. freeze_includes = _pytest.genscript.freeze_includes
  51. except ImportError:
  52. pass
  53. try:
  54. import _pytest.debugging
  55. set_trace = _pytest.debugging.pytestPDB().set_trace
  56. except ImportError:
  57. try:
  58. import _pytest.pdb
  59. set_trace = _pytest.pdb.pytestPDB().set_trace
  60. except ImportError:
  61. pass
  62. try:
  63. import _pytest.fixtures
  64. fixture = _pytest.fixtures.fixture
  65. yield_fixture = _pytest.fixtures.yield_fixture
  66. except ImportError:
  67. try:
  68. import _pytest.python
  69. fixture = _pytest.python.fixture
  70. yield_fixture = _pytest.python.yield_fixture
  71. except ImportError:
  72. pass
  73. """
  74. )
  75. register_module_extender(AstroidManager(), "pytest", pytest_transform)
  76. register_module_extender(AstroidManager(), "py.test", pytest_transform)