Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

conftest.py 3.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import shutil
  3. from pathlib import Path
  4. import pytest
  5. TMP = Path(__file__).resolve().parent / 'tmp' # temp directory for test files
  6. def pytest_addoption(parser):
  7. """
  8. Add custom command-line options to pytest.
  9. Args:
  10. parser (pytest.config.Parser): The pytest parser object.
  11. """
  12. parser.addoption('--slow', action='store_true', default=False, help='Run slow tests')
  13. def pytest_configure(config):
  14. """
  15. Register custom markers to avoid pytest warnings.
  16. Args:
  17. config (pytest.config.Config): The pytest config object.
  18. """
  19. config.addinivalue_line('markers', 'slow: mark test as slow to run')
  20. def pytest_runtest_setup(item):
  21. """
  22. Setup hook to skip tests marked as slow if the --slow option is not provided.
  23. Args:
  24. item (pytest.Item): The test item object.
  25. """
  26. if 'slow' in item.keywords and not item.config.getoption('--slow'):
  27. pytest.skip('skip slow tests unless --slow is set')
  28. def pytest_collection_modifyitems(config, items):
  29. """
  30. Modify the list of test items to remove tests marked as slow if the --slow option is not provided.
  31. Args:
  32. config (pytest.config.Config): The pytest config object.
  33. items (list): List of test items to be executed.
  34. """
  35. if not config.getoption('--slow'):
  36. # Remove the item entirely from the list of test items if it's marked as 'slow'
  37. items[:] = [item for item in items if 'slow' not in item.keywords]
  38. def pytest_sessionstart(session):
  39. """
  40. Initialize session configurations for pytest.
  41. This function is automatically called by pytest after the 'Session' object has been created but before performing
  42. test collection. It sets the initial seeds and prepares the temporary directory for the test session.
  43. Args:
  44. session (pytest.Session): The pytest session object.
  45. """
  46. from ultralytics.utils.torch_utils import init_seeds
  47. init_seeds()
  48. shutil.rmtree(TMP, ignore_errors=True) # delete any existing tests/tmp directory
  49. TMP.mkdir(parents=True, exist_ok=True) # create a new empty directory
  50. def pytest_terminal_summary(terminalreporter, exitstatus, config):
  51. """
  52. Cleanup operations after pytest session.
  53. This function is automatically called by pytest at the end of the entire test session. It removes certain files
  54. and directories used during testing.
  55. Args:
  56. terminalreporter (pytest.terminal.TerminalReporter): The terminal reporter object.
  57. exitstatus (int): The exit status of the test run.
  58. config (pytest.config.Config): The pytest config object.
  59. """
  60. from ultralytics.utils import WEIGHTS_DIR
  61. # Remove files
  62. models = [path for x in ['*.onnx', '*.torchscript'] for path in WEIGHTS_DIR.rglob(x)]
  63. for file in ['bus.jpg', 'yolov8n.onnx', 'yolov8n.torchscript'] + models:
  64. Path(file).unlink(missing_ok=True)
  65. # Remove directories
  66. models = [path for x in ['*.mlpackage', '*_openvino_model'] for path in WEIGHTS_DIR.rglob(x)]
  67. for directory in [TMP.parents[1] / '.pytest_cache', TMP] + models:
  68. shutil.rmtree(directory, ignore_errors=True)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...