-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitTest.cs
More file actions
209 lines (187 loc) · 9.08 KB
/
Copy pathUnitTest.cs
File metadata and controls
209 lines (187 loc) · 9.08 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using tunit.Properties;
namespace tunit {
public class UnitTest {
public UnitTest(TUnitProject tunitProject, string fileName) {
if (!IsTUnitApplication(fileName)) throw new ArgumentException($"{fileName} is not a TUnit application");
this.FileName = fileName;
this.tunitProject = tunitProject;
this.Reset();
}
public TimeSpan Duration { get; set; }
public string FileName { get; private set; }
public int TestCount {
get {
int count = 0;
foreach (var testFixture in this.testFixtures) {
count += testFixture.Value.TestCount;
}
return count;
}
}
public string Name {
get { return System.IO.Path.GetFileNameWithoutExtension(this.FileName); }
}
public TestFixture this[string testFixtureName] {
get { return this.testFixtures[testFixtureName]; }
}
public string[] TestFixtureNames {
get { return this.testFixtures.Keys.ToArray(); }
}
public TestFixture[] TestFixtures {
get { return this.testFixtures.Values.ToArray(); }
}
public string[] TextOutput { get ; private set; }
public TUnitProject TUnitProject {
get { return this.tunitProject; }
}
public TestStatus Status { get; set; }
public static bool IsTUnitApplication(string fileName) {
if (!System.IO.File.Exists(fileName)) return false;
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = new System.Diagnostics.ProcessStartInfo(fileName, "--help");
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
if (process.WaitForExit(1000) == false) return false;
if (!process.StandardOutput.ReadToEnd().StartsWith("This program contains tests written using xtd::tunit.")) return false;
if (!process.HasExited) process.Kill();
process.Close();
return true;
}
public void Load() {
this.process = new System.Diagnostics.Process();
this.process.StartInfo = new System.Diagnostics.ProcessStartInfo(this.FileName, "--list_tests");
this.process.StartInfo.CreateNoWindow = true;
this.process.StartInfo.UseShellExecute = false;
this.process.StartInfo.RedirectStandardOutput = true;
this.process.Start();
while (!process.StandardOutput.EndOfStream) {
string[] test = this.process.StandardOutput.ReadLine().Split('.');
if (!this.testFixtures.ContainsKey(test[0])) this.testFixtures.Add(test[0], new TestFixture(this, test[0]));
this.testFixtures[test[0]].AddTest(test[1]);
}
this.process.WaitForExit();
this.process.Close();
this.process = null;
}
public void Reset() {
this.Status = TestStatus.NotStarted;
this.TextOutput = new string[0];
foreach (var testFixture in this.testFixtures)
testFixture.Value.Reset();
}
public void Run() {
this.Run("");
}
private string MakeArguments() {
var result = "--output_color=false ";
if (Settings.Default.AlsoRunIgnoredTests) result += "--also_run_ignored_tests ";
if (Settings.Default.RepeatForEver) result += "--break_on_failure --repeat_tests=-1 ";
else if (Settings.Default.RepeatTests != 1) result += $"--break_on_failure --repeat_tests={Settings.Default.RepeatTests} ";
if (Settings.Default.ShuffleTests) result += "--shuffle_tests ";
if (Settings.Default.ShuffleTests && Settings.Default.RandomSeed != 0) result += $"--random_seed={Settings.Default.RandomSeed} ";
return result;
}
public void Run(string arguments) {
this.process = new System.Diagnostics.Process();
this.process.StartInfo = new System.Diagnostics.ProcessStartInfo(this.FileName, $"{MakeArguments()}{arguments}");
this.process.StartInfo.CreateNoWindow = true;
this.process.StartInfo.UseShellExecute = false;
this.process.StartInfo.RedirectStandardOutput = true;
this.process.Start();
ParseProcessOutput(this.process.StandardOutput);
this.process.WaitForExit();
this.process.Close();
this.process = null;
}
private void ParseProcessOutput(System.IO.StreamReader streamReader) {
List<string> lines = new List<string>();
string line;
List<string> errorsAndFailures = new List<string>();
string stackTrace = "";
string[] fixtureAndTestName = null;
TimeSpan duration = TimeSpan.Zero;
int countLine = 0;
TestStatus status = TestStatus.NotStarted;
while ((line = streamReader.ReadLine()) != null) {
lines.Add(line);
line = line.Trim();
if (line.StartsWith("FAILED ")) line = " " + line;
if (countLine++ < 2 || line == "") continue;
if (line.StartsWith("SUCCEED") || line.StartsWith("IGNORED") || line.StartsWith("ABORTED") || line.StartsWith(" FAILED")) {
if (status != TestStatus.NotStarted) {
if (this[fixtureAndTestName[0]][fixtureAndTestName[1]].Status != status) {
switch (this[fixtureAndTestName[0]][fixtureAndTestName[1]].Status) {
case TestStatus.Succeed: this.tunitProject.SucceedCount--; this.tunitProject.RanCount--; break;
case TestStatus.Ignored: this.tunitProject.IngoredCount--; this.tunitProject.RanCount--; break;
case TestStatus.Aborted: this.tunitProject.AbortedCount--; this.tunitProject.RanCount--; break;
case TestStatus.Failed: this.tunitProject.FailedCount--; this.tunitProject.RanCount--; break;
}
switch (status) {
case TestStatus.Succeed: this.tunitProject.SucceedCount++; this.tunitProject.RanCount++; break;
case TestStatus.Ignored: this.tunitProject.IngoredCount++; this.tunitProject.RanCount++; break;
case TestStatus.Aborted: this.tunitProject.AbortedCount++; this.tunitProject.RanCount++; break;
case TestStatus.Failed: this.tunitProject.FailedCount++; this.tunitProject.RanCount++; break;
}
}
this[fixtureAndTestName[0]][fixtureAndTestName[1]].SetStatus(status, errorsAndFailures.ToArray(), stackTrace, duration);
stackTrace = "";
errorsAndFailures.Clear();
status = TestStatus.NotStarted;
}
switch (line.Substring(0, 7)) {
case "SUCCEED": status = TestStatus.Succeed; break;
case "IGNORED": status = TestStatus.Ignored; break;
case "ABORTED": status = TestStatus.Aborted; break;
case " FAILED": status = TestStatus.Failed; break;
}
var milliseconds = 0.0;
double.TryParse(line.Substring(line.IndexOf("[") + 1, line.IndexOf(" ms") - line.IndexOf("[")), out milliseconds);
duration = TimeSpan.FromMilliseconds(milliseconds);
line = line.Substring(8, line.IndexOf(" [") - 8);
fixtureAndTestName = line.Split('.');
} else if (line.StartsWith("Stack Trace: in ")) {
stackTrace = line.Substring(16);
} else if (line.StartsWith("Test results:")) {
lines.AddRange(streamReader.ReadToEnd().Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries));
} else {
errorsAndFailures.Add(line.Trim());
}
}
if (status != TestStatus.NotStarted) {
if (this[fixtureAndTestName[0]][fixtureAndTestName[1]].Status != status) {
switch (this[fixtureAndTestName[0]][fixtureAndTestName[1]].Status) {
case TestStatus.Succeed: this.tunitProject.SucceedCount--; this.tunitProject.RanCount--; break;
case TestStatus.Ignored: this.tunitProject.IngoredCount--; this.tunitProject.RanCount--; break;
case TestStatus.Aborted: this.tunitProject.AbortedCount--; this.tunitProject.RanCount--; break;
case TestStatus.Failed: this.tunitProject.FailedCount--; this.tunitProject.RanCount--; break;
}
switch (status) {
case TestStatus.Succeed: this.tunitProject.SucceedCount++; this.tunitProject.RanCount++; break;
case TestStatus.Ignored: this.tunitProject.IngoredCount++; this.tunitProject.RanCount++; break;
case TestStatus.Aborted: this.tunitProject.AbortedCount++; this.tunitProject.RanCount++; break;
case TestStatus.Failed: this.tunitProject.FailedCount++; this.tunitProject.RanCount++; break;
}
}
this[fixtureAndTestName[0]][fixtureAndTestName[1]].SetStatus(status, errorsAndFailures.ToArray(), stackTrace, duration);
}
if (this.TextOutput.Length != 0) lines.Insert(0, "");
lines.InsertRange(0, this.TextOutput);
this.TextOutput = lines.ToArray();
}
public void Stop() {
if (this.process != null && !this.process.HasExited)
this.process.Kill();
}
private Dictionary<string, TestFixture> testFixtures = new Dictionary<string, TestFixture>();
private TUnitProject tunitProject;
System.Diagnostics.Process process = null;
}
}