test_tools.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import pytest
  2. import sys
  3. from library.python.pytest import config
  4. from library.python.pytest import yatest_tools
  5. config.set_test_mode()
  6. @pytest.fixture(params=["", "[param1,param2]"])
  7. def parameters(request):
  8. return request.param
  9. @pytest.mark.parametrize(
  10. "node_id,expected_class_name,expected_test_name",
  11. (
  12. ("package.test_script.py::test_name", "package.test_script.py", "test_name"),
  13. ("package.test_script.py", "package.test_script.py", "package.test_script.py"),
  14. ("package.test_script.py::class_name::test_name", "package.test_script.py::class_name", "test_name"),
  15. (
  16. "package.test_script.py::class_name::subclass_name::test_name",
  17. "package.test_script.py::class_name",
  18. "test_name",
  19. ),
  20. ),
  21. )
  22. def test_split_node_id_without_path(parameters, node_id, expected_class_name, expected_test_name):
  23. got = yatest_tools.split_node_id(node_id + parameters)
  24. assert (expected_class_name, expected_test_name + parameters) == got
  25. @pytest.mark.parametrize(
  26. "node_id,expected_class_name,expected_test_name",
  27. (
  28. ("/arcadia/root/package/test_script.py", "package.test_script.py", "package.test_script.py"),
  29. ("/arcadia/root/package/test_script.py::test_name", "package.test_script.py", "test_name"),
  30. (
  31. "/arcadia/root/package/test_script.py::class_name::test_name",
  32. "package.test_script.py::class_name",
  33. "test_name",
  34. ),
  35. (
  36. "/arcadia/root/package/test_script.py::class_name::subclass_name::test_name",
  37. "package.test_script.py::class_name",
  38. "test_name",
  39. ),
  40. ),
  41. )
  42. def test_split_node_id_with_path(mocker, parameters, node_id, expected_class_name, expected_test_name):
  43. mocker.patch.object(sys, 'extra_modules', sys.extra_modules | {'__tests__.package.test_script'})
  44. got = yatest_tools.split_node_id(node_id + parameters)
  45. assert (expected_class_name, expected_test_name + parameters) == got
  46. def test_missing_module(parameters):
  47. # An errno must be raised if module is not found in sys.extra_modules
  48. with pytest.raises(yatest_tools.MissingTestModule):
  49. yatest_tools.split_node_id("/arcadia/root/package/test_script2.py::test_name" + parameters)
  50. @pytest.mark.parametrize(
  51. "node_id,expected_class_name,expected_test_name",
  52. (
  53. ("package.test_script.py::test_name", "package.test_script.py", "test_suffix"),
  54. ("package.test_script.py", "package.test_script.py", "test_suffix"),
  55. ("package.test_script.py::class_name::test_name", "package.test_script.py", "test_suffix"),
  56. ("package.test_script.py::class_name::subclass_name::test_name", "package.test_script.py", "test_suffix"),
  57. ),
  58. )
  59. def test_split_node_id_with_test_suffix(parameters, node_id, expected_class_name, expected_test_name):
  60. got = yatest_tools.split_node_id(node_id + parameters, "test_suffix")
  61. assert (expected_class_name, expected_test_name + parameters) == got
  62. @pytest.mark.parametrize(
  63. "node_id,expected_class_name,expected_test_name",
  64. [
  65. ("/arcadia/data/b/a/test.py::test_b_a", "b.a.test.py", "test_b_a"),
  66. ("/arcadia/data/a/test.py::test_a", "a.test.py", "test_a"),
  67. ("/arcadia/data/test.py::test", "test.py", "test"),
  68. ("b/a/test.py::test_b_a", "b.a.test.py", "test_b_a"),
  69. ],
  70. )
  71. def test_path_resolving_for_local_conftest_load_policy(
  72. mocker, parameters, node_id, expected_class_name, expected_test_name
  73. ):
  74. # Order matters
  75. extra_modules = [
  76. '__tests__.b.a.test',
  77. '__tests__.test',
  78. '__tests__.a.test',
  79. ]
  80. mocker.patch.object(sys, 'extra_modules', extra_modules)
  81. got = yatest_tools.split_node_id(node_id + parameters)
  82. assert (expected_class_name, expected_test_name + parameters) == got
  83. DATA = [
  84. (
  85. "simple_bt",
  86. """
  87. path/test.py:15: in test
  88. return foo(0)
  89. path/test.py:5: in foo
  90. b, a = 1, 1 / p
  91. E ZeroDivisionError: integer division or modulo by zero
  92. """,
  93. ),
  94. (
  95. "nested_by",
  96. """
  97. path/test.py:13: in test
  98. raise Exception("123\n123\npath/test.py:15: in test")
  99. E Exception: 123
  100. E 123
  101. E path/test.py:15: in test
  102. During handling of the above exception, another exception occurred:
  103. path/test.py:15: in test
  104. return foo(0)
  105. path/test.py:5: in foo
  106. b, a = 1, 1 / p
  107. E ZeroDivisionError: division by zero
  108. """,
  109. ),
  110. ]
  111. @pytest.mark.parametrize("bt", [x[1] for x in DATA], ids=([x[0] for x in DATA]))
  112. def test_colorize_pytest_error(bt):
  113. return yatest_tools.colorize_pytest_error(bt)