Skip to content

Commit 320e765

Browse files
committed
Fix failure to capture test failures
1 parent 5f90197 commit 320e765

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

test/Microsoft.ML.TestFramework/BaseTestBaseline.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.IO;
8+
using System.Runtime.ExceptionServices;
89
using System.Text;
910
using System.Text.RegularExpressions;
1011
using System.Threading;
@@ -78,7 +79,7 @@ protected BaseTestBaseline(ITestOutputHelper output) : base(output)
7879
protected IHostEnvironment Env => _env;
7980
protected MLContext ML;
8081
private bool _normal;
81-
private bool _passed;
82+
private readonly List<Exception> _failures = new List<Exception>();
8283

8384
protected override void Initialize()
8485
{
@@ -96,7 +97,6 @@ protected override void Initialize()
9697

9798
string logPath = Path.Combine(logDir, FullTestName + LogSuffix);
9899
LogWriter = OpenWriter(logPath);
99-
_passed = true;
100100
_env = new ConsoleEnvironment(42, outWriter: LogWriter, errWriter: LogWriter)
101101
.AddStandardComponents();
102102
ML = new MLContext(42);
@@ -113,7 +113,7 @@ protected override void Cleanup()
113113
Contracts.Assert(IsActive);
114114
Log("Test {0}: {1}: {2}", TestName,
115115
_normal ? "completed normally" : "aborted",
116-
_passed ? "passed" : "failed");
116+
IsPassing ? "passed" : "failed");
117117

118118
Contracts.AssertValue(LogWriter);
119119
LogWriter.Dispose();
@@ -124,7 +124,7 @@ protected override void Cleanup()
124124

125125
protected bool IsActive { get { return LogWriter != null; } }
126126

127-
protected bool IsPassing { get { return _passed; } }
127+
protected bool IsPassing { get { return _failures.Count == 0; } }
128128

129129
// Called by a test to signal normal completion. If this is not called before the
130130
// TestScope is disposed, we assume the test was aborted.
@@ -134,7 +134,18 @@ protected void Done()
134134
Contracts.Assert(!_normal, "Done() should only be called once!");
135135
_normal = true;
136136

137-
Assert.True(_passed);
137+
switch (_failures.Count)
138+
{
139+
case 0:
140+
break;
141+
142+
case 1:
143+
ExceptionDispatchInfo.Capture(_failures[0]).Throw();
144+
break;
145+
146+
default:
147+
throw new AggregateException(_failures.ToArray());
148+
}
138149
}
139150

140151
protected bool Check(bool f, string msg)
@@ -164,8 +175,14 @@ protected void Fail(string fmt, params object[] args)
164175
protected void Fail(bool relax, string fmt, params object[] args)
165176
{
166177
Contracts.Assert(IsActive);
167-
if (!relax)
168-
_passed = false;
178+
try
179+
{
180+
throw new InvalidOperationException(string.Format(fmt, args));
181+
}
182+
catch (Exception ex) when (relax)
183+
{
184+
_failures.Add(ex);
185+
}
169186

170187
Log("*** Failure: " + fmt, args);
171188
}
@@ -210,12 +227,6 @@ protected string GetBaselinePath(string subDir, string name)
210227
return Path.GetFullPath(Path.Combine(_baselineBuildStringDir, subDir, name));
211228
}
212229

213-
// Inverts the _passed flag. Do not ever use this except in rare conditions. Eg. Recording failure of a test as a success.
214-
protected void DoNotEverUseInvertPass()
215-
{
216-
_passed = !_passed;
217-
}
218-
219230
// These are used to normalize output.
220231
private static readonly Regex _matchDataRoot = new Regex(DataRootRegExp, RegexOptions.IgnoreCase | RegexOptions.Compiled);
221232
private static readonly Regex _matchDataUnixRoot = new Regex(DataRootUnixRegExp, RegexOptions.IgnoreCase | RegexOptions.Compiled);

0 commit comments

Comments
 (0)