Skip to content

Commit 95acc60

Browse files
authored
Fix tests that break python-repeat (#5431)
I use python-repeat to check for flaky tests. It allows you to pass in `--count XXX` to repeat a test multiple times. Interestingly, for parameterized tests the decorator that creates the parameterized tests is run once globally, and then the tests are repeated. This means that these tests can catch places where the parameterized values are mutated in some way during a test. This fixes the cases found from `check/pytest --count 100`. In general mutating parameters called to functions without being very explicit about it is something we should try to avoid.
1 parent 1b7b9e6 commit 95acc60

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

cirq-core/cirq/contrib/acquaintance/topological_sort_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
@pytest.mark.parametrize(
2222
'circuit_dag,sorted_nodes',
2323
[
24-
(dag, cca.random_topological_sort(dag))
24+
(dag, tuple(cca.random_topological_sort(dag)))
2525
for dag in [
2626
cirq.CircuitDag.from_circuit(cirq.testing.random_circuit(10, 10, 0.5)) for _ in range(5)
2727
]

cirq-core/cirq/study/sweeps_test.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,18 @@ def test_access_sweep():
134134
assert sixth_elem == cirq.ParamResolver({'a': 2, 'b': 5})
135135

136136

137+
# We use factories since some of these produce generators and we want to
138+
# test for passing in a generator to initializer.
137139
@pytest.mark.parametrize(
138-
'r_list',
140+
'r_list_factory',
139141
[
140-
[{'a': a, 'b': a + 1} for a in (0, 0.5, 1, -10)],
141-
({'a': a, 'b': a + 1} for a in (0, 0.5, 1, -10)),
142-
({sympy.Symbol('a'): a, 'b': a + 1} for a in (0, 0.5, 1, -10)),
142+
lambda: [{'a': a, 'b': a + 1} for a in (0, 0.5, 1, -10)],
143+
lambda: ({'a': a, 'b': a + 1} for a in (0, 0.5, 1, -10)),
144+
lambda: ({sympy.Symbol('a'): a, 'b': a + 1} for a in (0, 0.5, 1, -10)),
143145
],
144146
)
145-
def test_list_sweep(r_list):
146-
sweep = cirq.ListSweep(r_list)
147+
def test_list_sweep(r_list_factory):
148+
sweep = cirq.ListSweep(r_list_factory())
147149
assert sweep.keys == ['a', 'b']
148150
assert len(sweep) == 4
149151
assert len(list(sweep)) == 4

cirq-core/cirq/testing/circuit_compare_test.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ def test_random_same_matrix(circuit):
133133

134134
cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent(circuit, same)
135135

136-
circuit.append(cirq.measure(a))
136+
mutable_circuit = circuit.copy()
137+
mutable_circuit.append(cirq.measure(a))
137138
same.append(cirq.measure(a))
138-
cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent(circuit, same)
139+
cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent(mutable_circuit, same)
139140

140141

141142
def test_correct_qubit_ordering():

0 commit comments

Comments
 (0)