Skip to content

Commit 18bfdc3

Browse files
authored
Various kinds of flaky tests. (#49)
1 parent da77c8b commit 18bfdc3

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

FlakyTests/AppCode/Class1.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ public class Class1
1010
{
1111
public int addFlaky(int x, int y)
1212
{
13-
14-
//compute the actual sum.
13+
// Here, this business logic itself is flaky.
14+
//
15+
// compute the actual sum.
1516
// then genereate a random number below 1000. If that number is odd, then add 1 to the sum.
1617
// return the sum
1718
int sum = x + y;
@@ -23,7 +24,7 @@ public int addFlaky(int x, int y)
2324
return sum;
2425
}
2526

26-
public int add(int x, int y)
27+
public int addStable(int x, int y)
2728
{
2829
int sum = x + y;
2930
return sum;

FlakyTests/AppTestProject1/UnitTest1.cs

+15-4
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,38 @@ namespace AppTestProject1
66
[TestClass]
77
public class UnitTest1
88
{
9+
// ------------------------
10+
// Flaky SUT business logic
11+
// ------------------------
912
[TestMethod]
1013
public void TestIsFlakyCozSUTisFalky()
1114
{
12-
// this test is flaky because the SUT is flaky.
15+
// this test is flaky because the SUT business logic is flaky.
1316
int x = 3;
1417
int y = 4;
1518
int sum = 7;
16-
1719
int val = new AppCode.Class1().addFlaky(x, y);
1820
Assert.AreEqual(sum, val);
1921
}
2022

21-
private static int state = new System.Random().Next(2);
23+
// ------------------------
24+
// Flaky environment
25+
// ------------------------
26+
private static int state = new System.Random().Next(2); // simulate a flaky environment.
2227

2328
[TestMethod]
2429
public void TestIsFlakyCozEnvIsFlaky()
2530
{
2631
// this test is flaky because the environment is flaky
27-
Assert.AreEqual(state, 0);
32+
int val = state;
33+
Assert.AreEqual(val, 0);
2834
}
2935

36+
37+
// -----------------------------------------------------
38+
// Flaky due to dependence on execution order
39+
// This a varition of the case of the flaky environment.
40+
// -----------------------------------------------------
3041
private static bool parentRan = false;
3142

3243
[TestMethod]

0 commit comments

Comments
 (0)