conftest.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright 2020 The HuggingFace Team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # tests directory-specific settings - this file is run automatically
  15. # by pytest before any tests are run
  16. import doctest
  17. import sys
  18. import warnings
  19. from os.path import abspath, dirname, join
  20. import _pytest
  21. from transformers.testing_utils import HfDoctestModule, HfDocTestParser
  22. # allow having multiple repository checkouts and not needing to remember to rerun
  23. # 'pip install -e .[dev]' when switching between checkouts and running tests.
  24. git_repo_path = abspath(join(dirname(__file__), "src"))
  25. sys.path.insert(1, git_repo_path)
  26. # silence FutureWarning warnings in tests since often we can't act on them until
  27. # they become normal warnings - i.e. the tests still need to test the current functionality
  28. warnings.simplefilter(action="ignore", category=FutureWarning)
  29. def pytest_configure(config):
  30. config.addinivalue_line(
  31. "markers", "is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested"
  32. )
  33. config.addinivalue_line(
  34. "markers", "is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested"
  35. )
  36. config.addinivalue_line("markers", "is_pipeline_test: mark test to run only when pipelines are tested")
  37. config.addinivalue_line("markers", "is_staging_test: mark test to run only in the staging environment")
  38. config.addinivalue_line("markers", "accelerate_tests: mark test that require accelerate")
  39. config.addinivalue_line("markers", "tool_tests: mark the tool tests that are run on their specific schedule")
  40. def pytest_addoption(parser):
  41. from transformers.testing_utils import pytest_addoption_shared
  42. pytest_addoption_shared(parser)
  43. def pytest_terminal_summary(terminalreporter):
  44. from transformers.testing_utils import pytest_terminal_summary_main
  45. make_reports = terminalreporter.config.getoption("--make-reports")
  46. if make_reports:
  47. pytest_terminal_summary_main(terminalreporter, id=make_reports)
  48. def pytest_sessionfinish(session, exitstatus):
  49. # If no tests are collected, pytest exists with code 5, which makes the CI fail.
  50. if exitstatus == 5:
  51. session.exitstatus = 0
  52. # Doctest custom flag to ignore output.
  53. IGNORE_RESULT = doctest.register_optionflag("IGNORE_RESULT")
  54. OutputChecker = doctest.OutputChecker
  55. class CustomOutputChecker(OutputChecker):
  56. def check_output(self, want, got, optionflags):
  57. if IGNORE_RESULT & optionflags:
  58. return True
  59. return OutputChecker.check_output(self, want, got, optionflags)
  60. doctest.OutputChecker = CustomOutputChecker
  61. _pytest.doctest.DoctestModule = HfDoctestModule
  62. doctest.DocTestParser = HfDocTestParser