Skip to content

Commit 68b404b

Browse files
authored
修复数据驱动中包含[]问题 (#64)
增加更多测试
1 parent f1b54e9 commit 68b404b

9 files changed

Lines changed: 58 additions & 15 deletions

File tree

‎pytest/pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "testsolar-pytestx"
3-
version = "0.1.16"
3+
version = "0.1.17"
44
description = "Pytest tool for TestSolar"
55
authors = [
66
{name = "asiazhang2002", email = "asiazhang2002@gmail.com"},

‎pytest/src/testsolar_pytestx/converter.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,14 @@ def decode_datadrive(name: str) -> str:
104104
https://docs.pytest.org/en/7.0.x/how-to/parametrize.html
105105
106106
test_include[\u4e2d\u6587-\u4e2d\u6587\u6c49\u5b57] -> test_include[中文-中文汉字]
107+
108+
用例名称中不允许出现[,因此如果有,一定是数据驱动的开头
107109
"""
108-
if name.endswith("]") and "[" in name:
109-
name = name.replace("[", "/[")
110+
if name.endswith("]"):
111+
start_index = name.find("[")
112+
if start_index != -1:
113+
name = name.replace(name[start_index], "/[", 1)
114+
110115
if re.search(r"\\u\w{4}", name):
111116
name = name.encode().decode("unicode_escape")
112117

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"TaskId": "aa",
3+
"ProjectPath": "bb",
4+
"FileReportPath": "",
5+
"Collectors": [],
6+
"Context": {},
7+
"TestSelectors": [
8+
"test_normal_case.py?test_success",
9+
"test_data_drive.py?test_special_data_drive_name/[中文-分号+[id:32]]",
10+
"test_data_drive.py?test_special_data_drive_name/[中文-分号+[id:32:34]]",
11+
"aa/bb/cc/test_in_sub_class.py",
12+
"test_data_drive.py",
13+
"errors/test_import_error.py",
14+
"errors/test_load_error.py"
15+
]
16+
}

‎pytest/testdata/test_data_drive.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@
44
@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])
55
def test_eval(test_input, expected):
66
assert eval(test_input) == expected
7+
8+
9+
@pytest.mark.parametrize("test_input", ["中文-分号+[id:32]"])
10+
def test_special_data_drive_name(test_input):
11+
assert "bad" not in test_input

‎pytest/tests/test_collector.py‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_collect_testcases_when_selector_is_valid(self):
3535

3636
re = read_load_result(pipe_io)
3737

38-
self.assertEqual(len(re.Tests), 5)
38+
self.assertEqual(len(re.Tests), 6)
3939
self.assertEqual(len(re.LoadErrors), 2)
4040

4141
self.assertEqual(
@@ -44,13 +44,17 @@ def test_collect_testcases_when_selector_is_valid(self):
4444
self.assertEqual(re.Tests[1].Name, "test_data_drive.py?test_eval/[2+4-6]")
4545
self.assertEqual(re.Tests[2].Name, "test_data_drive.py?test_eval/[3+5-8]")
4646
self.assertEqual(re.Tests[3].Name, "test_data_drive.py?test_eval/[6*9-42]")
47+
self.assertEqual(
48+
re.Tests[4].Name,
49+
"test_data_drive.py?test_special_data_drive_name/[中文-分号+[id:32]]",
50+
)
4751

48-
self.assertEqual(re.Tests[4].Name, "test_normal_case.py?test_success")
49-
self.assertEqual(re.Tests[4].Attributes["owner"], "foo")
50-
self.assertEqual(re.Tests[4].Attributes["description"], "测试获取答案")
51-
self.assertEqual(re.Tests[4].Attributes["tag"], "high")
52+
self.assertEqual(re.Tests[5].Name, "test_normal_case.py?test_success")
53+
self.assertEqual(re.Tests[5].Attributes["owner"], "foo")
54+
self.assertEqual(re.Tests[5].Attributes["description"], "测试获取答案")
55+
self.assertEqual(re.Tests[5].Attributes["tag"], "high")
5256
self.assertEqual(
53-
re.Tests[4].Attributes["extra_attributes"], '[{"env": ["AA", "BB"]}]'
57+
re.Tests[5].Attributes["extra_attributes"], '[{"env": ["AA", "BB"]}]'
5458
)
5559

5660
self.assertEqual(
@@ -83,7 +87,7 @@ def test_collect_testcases_when_select_not_valid(self):
8387

8488
re = read_load_result(pipe_io)
8589

86-
self.assertEqual(len(re.Tests), 3)
90+
self.assertEqual(len(re.Tests), 4)
8791
self.assertEqual(len(re.LoadErrors), 1)
8892
self.assertIn(
8993
"test_not_exist.py does not exist, SKIP it", re.LoadErrors[0].message

‎pytest/tests/test_converter.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ def test_selector_to_pytest_with_utf8_string(self):
3939
"/data/tests/tests/test_data_drive_zh_cn.py::aa::bb::test_include[\\u4e2d\\u6587-\\u4e2d\\u6587\\u6c49\\u5b57]",
4040
)
4141

42+
re = selector_to_pytest(
43+
"/data/tests/tests/test_data_drive_zh_cn.py?aa/bb/test_include/[中文-[中文]汉字]"
44+
)
45+
self.assertEqual(
46+
re,
47+
"/data/tests/tests/test_data_drive_zh_cn.py::aa::bb::test_include[\\u4e2d\\u6587-[\\u4e2d\\u6587]\\u6c49\\u5b57]",
48+
)
49+
4250
def test_pytest_path_cls_to_selector(self):
4351
mock = MagicMock()
4452
mock.nodeid = None
@@ -65,15 +73,16 @@ def test_pytest_node_id_to_selector_without_datadrive(self):
6573
def test_pytest_node_id_to_selector_with_datadrive(self):
6674
mock = MagicMock()
6775
mock.nodeid = (
68-
"/data/tests/tests/test_data_drive_zh_cn.py::test_include[#?-#?^$%!]"
76+
"/data/tests/tests/test_data_drive_zh_cn.py::test_include[#?-[中文:203]]"
6977
)
7078
mock.path = None
7179
mock.cls = None
7280

7381
re = pytest_to_selector(mock, "/data/tests/")
7482

7583
self.assertEqual(
76-
re, "/data/tests/tests/test_data_drive_zh_cn.py?test_include/[#?-#?^$%!]"
84+
re,
85+
"/data/tests/tests/test_data_drive_zh_cn.py?test_include/[#?-[中文:203]]",
7786
)
7887

7988
def test_pytest_location_to_selector(self):

‎pytest/tests/test_load.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def test_collect_testcases_from_args(self):
2323
pipe_io.seek(0)
2424
re = read_load_result(pipe_io)
2525

26-
self.assertEqual(len(re.Tests), 5)
26+
self.assertEqual(len(re.Tests), 6)
27+
self.assertEqual(re.Tests[4].Name, "test_data_drive.py?test_special_data_drive_name/[中文-分号+[id:32]]")
2728

2829
def test_raise_error_when_param_is_invalid(self):
2930
with self.assertRaises(SystemExit):

‎pytest/tests/test_run.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ class TestExecuteEntry(TestCase):
1717
def test_run_testcases_from_args(self):
1818
pipe_io = io.BytesIO()
1919
run_testcases_from_args(
20-
args=["run.py", Path.joinpath(Path(self.testdata_dir), "entry.json")],
20+
args=[
21+
"run.py",
22+
Path.joinpath(Path(self.testdata_dir), "entry_with_error_case.json"),
23+
],
2124
workspace=self.testdata_dir,
2225
pipe_io=pipe_io,
2326
)

‎pytest/testtool.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ schemaVersion: 1.0
22
name: pytest
33
nameZh: pytest自动化测试
44
lang: python
5-
version: '0.1.16'
5+
version: '0.1.17'
66
langType: INTERPRETED
77
description: |-
88
pytest是一个成熟的全功能Python测试工具,可以帮助您编写更好的程序。此测试工具允许您在TestSolar上运行pytest。

0 commit comments

Comments
 (0)