# 测试 LLM 的 assert 功能

# 1_PythonProjectTemplate-master

运行 pip install -e .pytest tests 后出现下图,表明本地构建和安装和测试项目成功。

然后再运行 pytest --cov=fastvector tests --cov-report=html 来生成代码覆盖率报告

<img src="D:\desktop\Code_Compiling\LLM 的 test_oracle 生成 \0_readme_picture\1.png" alt="1" style="zoom:50%;" />

# test 代码修改

# test 初始代码

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
""" Unit Test Cases """
from typing import Any

import pytest
from fastvector.vector import Vector2D


V1 = Vector2D(0, 0)
V2 = Vector2D(-1, 1)
V3 = Vector2D(2.5, -2.5)


####################
# INIT #
####################


@pytest.mark.parametrize(
("x", "y"),
(
(-1, None),
(1, None),
(None, 1),
(None, -1),
),
)
def test_init_raises(
x: float,
y: float,
) -> None:
"""Test init raises."""
with pytest.raises(TypeError):
_ = Vector2D(x, y)


@pytest.mark.parametrize(
("x", "y", "exp"),
(
(-1, 1, Vector2D(-1, 1)),
(1, -1, Vector2D(1, -1)),
(1, 1, Vector2D(1, 1)),
),
)
def test_from_values(
x: float,
y: float,
exp: Vector2D,
) -> None:
"""Test from values."""
assert exp == Vector2D(x, y)


####################
# STRINGS #
####################


def test_repr(
capture_stdout: dict[str, Any],
) -> None:
"""Test repr."""
print(repr(Vector2D(1.0, 2.0)))
assert capture_stdout["stdout"] == "vector.Vector2D(1.0, 2.0)\n"


def test_str(
capture_stdout: dict[str, Any],
) -> None:
"""Test str."""
print(str(Vector2D(1.0, 2.0)))
assert capture_stdout["stdout"] == "(1.0, 2.0)\n"


####################
# COMPUTATIONS #
####################


@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, V2, Vector2D(-1, 1)),
(V1, V3, Vector2D(2.5, -2.5)),
(V3, V2, Vector2D(1.5, -1.5)),
),
)
def test_add(
lhs: Vector2D,
rhs: Vector2D,
exp_res: Vector2D,
) -> None:
"""Test add."""
assert lhs + rhs == exp_res


@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, V2, Vector2D(1, -1)),
(V1, V3, Vector2D(-2.5, 2.5)),
(V3, V2, Vector2D(3.5, -3.5)),
),
)
def test_sub(
lhs: Vector2D,
rhs: Vector2D,
exp_res: Vector2D,
) -> None:
"""Test sub."""
assert lhs - rhs == exp_res


@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, V2, 0.0),
(V1, V3, 0.0),
(V3, V2, -5.0),
),
)
def test_mul_vec(
lhs: Vector2D,
rhs: Vector2D,
exp_res: float,
) -> None:
"""Test mul vec."""
assert lhs * rhs == exp_res


@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, 2.0, Vector2D(0.0, 0.0)),
(V2, 2.0, Vector2D(-2.0, 2.0)),
(V3, 2.0, Vector2D(5.0, -5.0)),
),
)
def test_mul_float(
lhs: Vector2D,
rhs: float,
exp_res: Vector2D,
) -> None:
"""Test mul float."""
assert lhs * rhs == exp_res


@pytest.mark.parametrize(
("rhs", "lhs"),
(
(Vector2D(1, 1), None),
(Vector2D(1, 1), "1"),
),
)
def test_mul_raises(
rhs: Vector2D,
lhs: Any,
) -> None:
"""Test mul raises."""
with pytest.raises(TypeError):
rhs * lhs # pyright: ignore reportUnusedExpression


@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, 2.0, Vector2D(0.0, 0.0)),
(V2, 2.0, Vector2D(-0.5, 0.5)),
(V3, 2.0, Vector2D(1.25, -1.25)),
),
)
def test_div(
lhs: Vector2D,
rhs: float,
exp_res: Vector2D,
) -> None:
"""Test div."""
assert lhs / rhs == exp_res


@pytest.mark.parametrize(
("lhs", "rhs"), ((Vector2D(0.0, 0.0), Vector2D(0.0, 0.0)),)
)
def test_div_raises(
lhs: Vector2D,
rhs: float,
) -> None:
"""Test div raises."""
with pytest.raises(TypeError):
lhs / rhs # pyright: ignore reportUnusedExpression


@pytest.mark.parametrize(
("rhs", "lhs"),
(
(Vector2D(1, 1), (0, 1)),
(Vector2D(1, 1), [1, 0]),
),
)
def test_operators_raises(
rhs: Vector2D,
lhs: Vector2D,
) -> None:
"""Test operators raises."""
with pytest.raises(TypeError):
rhs < lhs # pyright: ignore reportUnusedExpression
with pytest.raises(TypeError):
rhs + lhs # pyright: ignore reportUnusedExpression
with pytest.raises(TypeError):
rhs - lhs # pyright: ignore reportUnusedExpression


@pytest.mark.parametrize(
("rhs", "lhs"),
(
(Vector2D(0, 0), 0),
(Vector2D(0, 1), 1),
(Vector2D(1, 0), 1),
),
)
def test_abs(
rhs: Vector2D,
lhs: float,
) -> None:
"""Test abs."""
assert abs(rhs) == lhs


####################
# COMPARISONS #
####################


@pytest.mark.parametrize(
("lhs", "rhs"),
(
(Vector2D(1, 1), (1, 1)),
(Vector2D(1, 1), [1, 1]),
),
)
def test_equality_other_class(
lhs: Vector2D,
rhs: object,
) -> None:
"""Test equality."""
assert lhs != rhs


@pytest.mark.parametrize(
("lhs", "rhs"),
(
(Vector2D(1, 1), Vector2D(0, 1)),
(Vector2D(1, 1), Vector2D(1, 0)),
),
)
def test_less_than(
lhs: Vector2D,
rhs: Vector2D,
) -> None:
"""Test less than."""
assert rhs < lhs

从 test 代码中我们可知:test_from_values、test_repr、test_str、test_add、test_sub、test_mul_vec、test_mul_float、test_div、test_abs、test_equality_other_class、test_less_than 中有 assert 语句。

将 assert 语句删除,并且删掉提示字符串,然后将代码交给 gpt3.5 进行修改。

提示语句为 “这个 test case 缺少 assert 语句 请补全”

# test 投喂代码

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
""" Unit Test Cases """
from typing import Any

import pytest
from fastvector.vector import Vector2D


V1 = Vector2D(0, 0)
V2 = Vector2D(-1, 1)
V3 = Vector2D(2.5, -2.5)


####################
# INIT #
####################


@pytest.mark.parametrize(
("x", "y"),
(
(-1, None),
(1, None),
(None, 1),
(None, -1),
),
)
def test_init_raises(
x: float,
y: float,
) -> None:
"""Test init raises."""
with pytest.raises(TypeError):
_ = Vector2D(x, y)


@pytest.mark.parametrize(
("x", "y", "exp"),
(
(-1, 1, Vector2D(-1, 1)),
(1, -1, Vector2D(1, -1)),
(1, 1, Vector2D(1, 1)),
),
)
def test_from_values(
x: float,
y: float,
exp: Vector2D,
) -> None:
pass


####################
# STRINGS #
####################


def test_repr(
capture_stdout: dict[str, Any],
) -> None:
print(repr(Vector2D(1.0, 2.0)))



def test_str(
capture_stdout: dict[str, Any],
) -> None:
print(str(Vector2D(1.0, 2.0)))


####################
# COMPUTATIONS #
####################


@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, V2, Vector2D(-1, 1)),
(V1, V3, Vector2D(2.5, -2.5)),
(V3, V2, Vector2D(1.5, -1.5)),
),
)
def test_add(
lhs: Vector2D,
rhs: Vector2D,
exp_res: Vector2D,
) -> None:
pass

@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, V2, Vector2D(1, -1)),
(V1, V3, Vector2D(-2.5, 2.5)),
(V3, V2, Vector2D(3.5, -3.5)),
),
)
def test_sub(
lhs: Vector2D,
rhs: Vector2D,
exp_res: Vector2D,
) -> None:
pass

@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, V2, 0.0),
(V1, V3, 0.0),
(V3, V2, -5.0),
),
)
def test_mul_vec(
lhs: Vector2D,
rhs: Vector2D,
exp_res: float,
) -> None:
pass

@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, 2.0, Vector2D(0.0, 0.0)),
(V2, 2.0, Vector2D(-2.0, 2.0)),
(V3, 2.0, Vector2D(5.0, -5.0)),
),
)
def test_mul_float(
lhs: Vector2D,
rhs: float,
exp_res: Vector2D,
) -> None:
pass

@pytest.mark.parametrize(
("rhs", "lhs"),
(
(Vector2D(1, 1), None),
(Vector2D(1, 1), "1"),
),
)
def test_mul_raises(
rhs: Vector2D,
lhs: Any,
) -> None:
"""Test mul raises."""
with pytest.raises(TypeError):
rhs * lhs # pyright: ignore reportUnusedExpression


@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, 2.0, Vector2D(0.0, 0.0)),
(V2, 2.0, Vector2D(-0.5, 0.5)),
(V3, 2.0, Vector2D(1.25, -1.25)),
),
)
def test_div(
lhs: Vector2D,
rhs: float,
exp_res: Vector2D,
) -> None:
pass

@pytest.mark.parametrize(
("lhs", "rhs"), ((Vector2D(0.0, 0.0), Vector2D(0.0, 0.0)),)
)
def test_div_raises(
lhs: Vector2D,
rhs: float,
) -> None:
"""Test div raises."""
with pytest.raises(TypeError):
lhs / rhs # pyright: ignore reportUnusedExpression


@pytest.mark.parametrize(
("rhs", "lhs"),
(
(Vector2D(1, 1), (0, 1)),
(Vector2D(1, 1), [1, 0]),
),
)
def test_operators_raises(
rhs: Vector2D,
lhs: Vector2D,
) -> None:
"""Test operators raises."""
with pytest.raises(TypeError):
rhs < lhs # pyright: ignore reportUnusedExpression
with pytest.raises(TypeError):
rhs + lhs # pyright: ignore reportUnusedExpression
with pytest.raises(TypeError):
rhs - lhs # pyright: ignore reportUnusedExpression


@pytest.mark.parametrize(
("rhs", "lhs"),
(
(Vector2D(0, 0), 0),
(Vector2D(0, 1), 1),
(Vector2D(1, 0), 1),
),
)
def test_abs(
rhs: Vector2D,
lhs: float,
) -> None:
pass

####################
# COMPARISONS #
####################


@pytest.mark.parametrize(
("lhs", "rhs"),
(
(Vector2D(1, 1), (1, 1)),
(Vector2D(1, 1), [1, 1]),
),
)
def test_equality_other_class(
lhs: Vector2D,
rhs: object,
) -> None:
pass

@pytest.mark.parametrize(
("lhs", "rhs"),
(
(Vector2D(1, 1), Vector2D(0, 1)),
(Vector2D(1, 1), Vector2D(1, 0)),
),
)
def test_less_than(
lhs: Vector2D,
rhs: Vector2D,
) -> None:
pass

# test 代码分析

gpt3.5 修改后的 test 语句为:

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
""" Unit Test Cases """
from typing import Any

import pytest
from fastvector.vector import Vector2D


V1 = Vector2D(0, 0)
V2 = Vector2D(-1, 1)
V3 = Vector2D(2.5, -2.5)


####################
# INIT #
####################


@pytest.mark.parametrize(
("x", "y"),
(
(-1, None),
(1, None),
(None, 1),
(None, -1),
),
)
def test_init_raises(
x: float,
y: float,
) -> None:
"""Test init raises."""
with pytest.raises(TypeError):
_ = Vector2D(x, y)


@pytest.mark.parametrize(
("x", "y", "exp"),
(
(-1, 1, Vector2D(-1, 1)),
(1, -1, Vector2D(1, -1)),
(1, 1, Vector2D(1, 1)),
),
)
def test_from_values(
x: float,
y: float,
exp: Vector2D,
) -> None:
assert Vector2D(x, y) == exp


####################
# STRINGS #
####################


def test_repr(
capture_stdout: dict[str, Any],
) -> None:
assert repr(Vector2D(1.0, 2.0)) == "Vector2D(1.0, 2.0)"


def test_str(
capture_stdout: dict[str, Any],
) -> None:
assert str(Vector2D(1.0, 2.0)) == "(1.0, 2.0)"


####################
# COMPUTATIONS #
####################


@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, V2, Vector2D(-1, 1)),
(V1, V3, Vector2D(2.5, -2.5)),
(V3, V2, Vector2D(1.5, -1.5)),
),
)
def test_add(
lhs: Vector2D,
rhs: Vector2D,
exp_res: Vector2D,
) -> None:
assert lhs + rhs == exp_res

@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, V2, Vector2D(1, -1)),
(V1, V3, Vector2D(-2.5, 2.5)),
(V3, V2, Vector2D(3.5, -3.5)),
),
)
def test_sub(
lhs: Vector2D,
rhs: Vector2D,
exp_res: Vector2D,
) -> None:
assert lhs - rhs == exp_res

@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, V2, 0.0),
(V1, V3, 0.0),
(V3, V2, -5.0),
),
)
def test_mul_vec(
lhs: Vector2D,
rhs: Vector2D,
exp_res: float,
) -> None:
assert lhs * rhs == exp_res

@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, 2.0, Vector2D(0.0, 0.0)),
(V2, 2.0, Vector2D(-2.0, 2.0)),
(V3, 2.0, Vector2D(5.0, -5.0)),
),
)
def test_mul_float(
lhs: Vector2D,
rhs: float,
exp_res: Vector2D,
) -> None:
assert lhs * rhs == exp_res

@pytest.mark.parametrize(
("rhs", "lhs"),
(
(Vector2D(1, 1), None),
(Vector2D(1, 1), "1"),
),
)
def test_mul_raises(
rhs: Vector2D,
lhs: Any,
) -> None:
"""Test mul raises."""
with pytest.raises(TypeError):
rhs * lhs # pyright: ignore reportUnusedExpression


@pytest.mark.parametrize(
("lhs", "rhs", "exp_res"),
(
(V1, 2.0, Vector2D(0.0, 0.0)),
(V2, 2.0, Vector2D(-0.5, 0.5)),
(V3, 2.0, Vector2D(1.25, -1.25)),
),
)
def test_div(
lhs: Vector2D,
rhs: float,
exp_res: Vector2D,
) -> None:
assert lhs / rhs == exp_res

@pytest.mark.parametrize(
("lhs", "rhs"), ((Vector2D(0.0, 0.0), Vector2D(0.0, 0.0)),)
)
def test_div_raises(
lhs: Vector2D,
rhs: float,
) -> None:
"""Test div raises."""
with pytest.raises(TypeError):
lhs / rhs # pyright: ignore reportUnusedExpression


@pytest.mark.parametrize(
("rhs", "lhs"),
(
(Vector2D(1, 1), (0, 1)),
(Vector2D(1, 1), [1, 0]),
),
)
def test_operators_raises(
rhs: Vector2D,
lhs: Vector2D,
) -> None:
"""Test operators raises."""
with pytest.raises(TypeError):
rhs < lhs # pyright: ignore reportUnusedExpression
with pytest.raises(TypeError):
rhs + lhs # pyright: ignore reportUnusedExpression
with pytest.raises(TypeError):
rhs - lhs # pyright: ignore reportUnusedExpression


@pytest.mark.parametrize(
("rhs", "lhs"),
(
(Vector2D(0, 0), 0),
(Vector2D(0, 1), 1),
(Vector2D(1, 0), 1),
),
)
def test_abs(
rhs: Vector2D,
lhs: float,
) -> None:
assert abs(rhs) == lhs

####################
# COMPARISONS #
####################


@pytest.mark.parametrize(
("lhs", "rhs"),
(
(Vector2D(1, 1), (1, 1)),
(Vector2D(1, 1), [1, 1]),
),
)
def test_equality_other_class(
lhs: Vector2D,
rhs: object,
) -> None:
assert lhs != rhs

@pytest.mark.parametrize(
("lhs", "rhs"),
(
(Vector2D(1, 1), Vector2D(0, 1)),
(Vector2D(1, 1), Vector2D(1, 0)),
),
)
def test_less_than(
lhs: Vector2D,
rhs: Vector2D,
) -> None:
assert lhs < rhs

# 比对修改前后代码块的作用

  1. 对于 test_init_raises() 函数,其用于验证创建的 Vector2D 对象是否与预期结果 exp 相等。—— 修改前后代码作用相同

  2. 对于 test_repr和test_str 函数:
    修改前:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    def test_repr(
    capture_stdout: dict[str, Any],
    ) -> None:
    """Test repr."""
    print(repr(Vector2D(1.0, 2.0)))
    assert capture_stdout["stdout"] == "vector.Vector2D(1.0, 2.0)\n"


    def test_str(
    capture_stdout: dict[str, Any],
    ) -> None:
    """Test str."""
    print(str(Vector2D(1.0, 2.0)))
    assert capture_stdout["stdout"] == "(1.0, 2.0)\n"

    其作用为验证 Vector2D 对象的 '__repr__' 方法的输出。通过建立字典 capture_stdout 来捕获 capture_stdout。print 函数的目的为将 Vector2D(1.0, 2.0) 对象的 __repr__ 方法的输出打印到标准输出。以便实际使用时开发者可以看到程序输出结果,帮助理解程序执行的结果和确保 __repr__ 方法的实现符合预期。 然后使用 assert capture_stdout["stdout"] == "vector.Vector2D(1.0, 2.0)\n " 来验证捕获的输出与预期输出是否相符。
    gpt3.5 补充后:

    1
    2
    3
    4
    5
    6
    7
    def test_repr() -> None:
    assert repr(Vector2D(1.0, 2.0)) == "Vector2D(1.0, 2.0)"


    def test_str() -> None:
    assert str(Vector2D(1.0, 2.0)) == "(1.0, 2.0)"

    缺点

    • 功能减少:一般来说为了符合开发者调试,test 时需要 print 函数以便开发者了解程序的输出,gpt 填充的 assert 里忽略了 print,直接对比返回值,其只适用于 reprstr 函数的输出场景
    • 调试困难:如果我们 assert 报错后,最初的代码可以通过 print 来留下更多的调试信息,而 gpt 生成的返回值无法做到这一步,需要额外增加 print 代码来了解报错原因

    改进:当我们给 gpt 输入:“这个 test case 缺少 assert 语句 请补全,要求调试的过程中方便开发者直观看到输出过程” 后,代码输出增加了 print 语句,优化成功。

  3. test_addtest_subtest_mul_float``test_div 和 'test_abs' 和 'test_less_than' 函数:验证两个 Vector2D 对象相加 / 减 / 乘 / 除 / 取绝对值 / 是否小于的结果是否与预期结果相等。gpt 改后与之前效果相同。

# LLM 生成代码分析

# LLM 修改后与原来一致:

  1. assert exp == Vector2D(x, y)
  2. assert capture_stdout["stdout"] == "vector.Vector2D(1.0, 2.0)
  3. assert capture_stdout["stdout"] == "(1.0, 2.0)\n"
  4. assert lhs + rhs == exp_res
  5. assert lhs - rhs == exp_res
  6. assert lhs * rhs == exp_res
  7. assert lhs * rhs == exp_res
  8. assert lhs / rhs == exp_res
  9. assert abs(rhs) == lhs
  10. assert lhs != rhs
  11. assert rhs < lhs

# LLM 修改后与原来不同:

# LLM 生成代码与原代码一致的概率

100%

# 分析一:断言在验证的东西

  1. assert exp == Vector2D(x, y)
    • 使用参数化设置,传入不同的 x 和 y 值,以及期望的 Vector2D (x,y) 对象 'exp'
    • 测试通过创建一个新的 Vector2D 对象并将其与预期的对象进行比较来验证初始化是否正确
  2. assert capture_stdout["stdout"] == "vector.Vector2D(1.0, 2.0)\n"
    • 验证 repr 方法返回的 Vector2D 对象是否用正确字符串表示
  3. assert capture_stdout["stdout"] == "(1.0, 2.0)\n"
    • 验证 str 方法返回的 Vector2D 对象是否用正确字符串表示
  4. assert lhs + rhs == exp_res
    • 验证两个 Vector2D 对象相加的结果是否正确。
  5. assert lhs - rhs == exp_res
    • 验证两个 Vector2D 对象相减的结果是否正确。
  6. assert lhs * rhs == exp_res
    • 验证两个 Vector2D 对象相乘(点积)的结果是否正确。
  7. assert lhs * rhs == exp_res —— test_mul_float
    • 验证 Vector2D 对象与浮点数相乘的结果是否正确。
  8. assert lhs / rhs == exp_res
    • Vector2D 对象与浮点数相除的结果是否正确。
  9. assert abs(rhs) == lhs
    • 验证 Vector2D 对象的绝对值(模)是否正确。
  10. assert lhs != rhs
    • 验证当 Vector2D 对象与不同类对象进行比较时,是否返回 False
  11. assert rhs < lhs
    • 验证 Vector2D 对象的 “小于” 比较运算是否正确。

# 分析二:断言是否依赖于具体的 SUT (被测软件) 的输出及原因

  1. assert exp == Vector2D(x, y)
    • 依赖
    • 这个测试通过创建 Vector2D 对象来验证其初始化功能
    • 断言 exp == Vector2D(x, y) 依赖于 Vector2D__eq__ 方法是否正确实现。
    • 如果 Vector2D 类的初始化或 __eq__ 方法有问题,这个断言将无法通过。因此,断言依赖于 Vector2D 类的具体实现(即 SUT 的输出)。
  2. assert capture_stdout["stdout"] == "vector.Vector2D(1.0, 2.0)\n"
    • 依赖
    • 断言 capture_stdout["stdout"] == "vector.Vector2D(1.0, 2.0)\n" 依赖于 __repr__ 方法的具体输出。
    • 也依赖 Vector2D (1.0,2.0) 的具体输出
  3. assert capture_stdout["stdout"] == "(1.0, 2.0)\n"
    • 依赖
    • 它依赖于 Vector2D 类中 __str__ 方法的具体实现。
  4. assert lhs + rhs == exp_res
    • 依赖
    • 依赖于 Vector2D 类中 __add__ 方法的具体实现。
  5. assert lhs - rhs == exp_res
    • 依赖
    • 依赖于 Vector2D 类中 __sub__ 方法的具体实现。
  6. assert lhs * rhs == exp_res
    • 依赖
    • 它依赖于 Vector2D 类中 __mul__ 方法对另一个 Vector2D 对象的具体实现。
  7. assert lhs * rhs == exp_res —— test_mul_float
    • 依赖
    • 依赖于 Vector2D 类中 __mul__ 方法对浮点数的具体实现。
  8. assert lhs / rhs == exp_res
    • 依赖
    • 依赖于 Vector2D 类中 __truediv__ 方法的具体实现
  9. assert abs(rhs) == lhs
    • 它依赖于 Vector2D 类中 __abs__ 方法的具体实现。
  10. assert lhs != rhs
    • 它依赖于 Vector2D 类中 __eq__ 方法的具体实现。
  11. assert rhs < lhs
    • 它依赖于 Vector2D 类中 __lt__ 方法的具体实现。

# 分析三:断言为何能生成

  1. assert exp == Vector2D(x, y)
    • 函数名:投喂给 LLM 的函数名为 test_from_values ,即从数值上进行比较,而函数中对 x,y 进行初始化,并且定义了一个 exp=Vector2D,故 LLM 可以判断断言为判断 exp 是否和 Vector2D (x,y) 相等
  2. assert capture_stdout["stdout"] == "vector.Vector2D(1.0, 2.0)\n"
    • 函数名,投喂给 LLM 的函数名为 test_repr ,即为测试该 Vector2D 的标准字符串表示
    • 在代码里出现了 capture_stdout,即为捕获标准输出。
    • 函数里出现 print(repr(Vector2D(1.0, 2.0))) ,即可知道需要对于标准输出和 repr(Vector2D(1.0, 2.0)) 进行对比
    • 故 LLM 可以依此来判定断言
  3. assert capture_stdout["stdout"] == "(1.0, 2.0)\n"
    • 函数名,投喂给 LLM 的函数名为 test_str ,即为测试该 Vector2D 的字符串表示
    • 在代码里出现了 capture_stdout,即为捕获标准输出。
    • 函数里出现 print(repr(Vector2D(1.0, 2.0))) ,即可知道需要对于标准输出和 str(Vector2D(1.0, 2.0)) 进行对比
    • 故 LLM 可以依此来判定断言
  4. assert lhs + rhs == exp_res
    • 函数名为 test_add , 即为测试 add 方法
    • 函数中出现了 lhs,rhs 和 exp_res
    • 故 LLM 可以判定方法为测试相加是否相等
  5. assert lhs - rhs == exp_res
    • 函数名为 test_sub , 即为测试 add 方法
    • 函数中出现了 lhs,rhs 和 exp_res
    • 故 LLM 可以判定方法为测试相减是否相等
  6. assert lhs * rhs == exp_res
    • 函数名为 test_mul_vec,即为测试相乘是否结果一致
    • 出现了 lhs,rhs,exp_res 的初始化定义
    • 故 LLM 可以判定方法为测试相乘是否相等
  7. assert lhs * rhs == exp_res —— test_mul_float
    • 函数名为 test_mul_vec,即为测试相乘是否结果一致
    • 出现了 lhs,rhs,exp_res 的初始化定义
    • 故 LLM 可以判定方法为测试 Vector2D 与浮点数相乘的结果
  8. assert lhs / rhs == exp_res
    • 函数名为 test_div ,即测试除法
    • 函数中对 lhs,rhs,exp_res 进行初始化
    • 即 LLM 可以判定为测试除法是否正确
  9. assert abs(rhs) == lhs
    • 函数名为 test_abs ,即测试绝对值
    • 函数中对 reh 和 lhs 进行初始化
    • 故可以判定方法为测试绝对值
  10. assert lhs != rhs
    • 函数名为 test_equality_other_class
    • 函数中定义了 lhs: Vector2Drhs: object
    • 故 LLM 可知方法为测试两个不同类进行比较是否返回 False
  11. assert rhs < lhs
    • 函数名为 test_less_than
    • 初始化定义了两个 Vector2D 类 lhs 和 rhs
    • 故 LLM 可知该断言应该为判断对象的小于比较是否正确