Skip to content

Commit c2cf422

Browse files
committed
Use ITestOutputHelper instead of Console.WriteLine
Messages logged by tests to the console using Console.WriteLine and Console.Error.WriteLine are not captured by newer versions of the .NET SDK. Instead, they get lost. So we get no real feedback for failing tests in logs. Messages logged by ITestOutputHelper are captured by the test runner, and automatically displayed by the test runner for failing tests. They don't get displayed for passing tests out of the box, but can be displayed by configuring the test logger.
1 parent 5043d97 commit c2cf422

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

assemblies-valid/AssembliesValid.cs

+14-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Text;
1212
using System.Text.RegularExpressions;
1313
using Xunit;
14+
using Xunit.Abstractions;
1415

1516
namespace AssembliesValid
1617
{
@@ -40,30 +41,37 @@ public class AssembliesValid
4041
new Regex("/packs/"),
4142
};
4243

44+
private readonly ITestOutputHelper output;
45+
46+
public AssembliesValid(ITestOutputHelper output)
47+
{
48+
this.output = output;
49+
}
50+
4351
[Fact]
4452
public void ValidateAssemblies()
4553
{
4654
string dotnetPath = null;
4755
int exitCode = RunProcessAndGetOutput(new string[] { "bash", "-c", "command -v dotnet" }, out dotnetPath);
4856
if (exitCode != 0)
4957
{
50-
Console.Error.WriteLine("'dotnet' command not found");
51-
Console.Error.WriteLine("PATH: " + Environment.GetEnvironmentVariable("PATH"));
58+
output.WriteLine("'dotnet' command not found");
59+
output.WriteLine("PATH: " + Environment.GetEnvironmentVariable("PATH"));
5260
Assert.True(false);
5361
}
5462
dotnetPath = dotnetPath.Trim();
5563
exitCode = RunProcessAndGetOutput(new string[] { "readlink", "-f", dotnetPath }, out dotnetPath);
5664
if (exitCode != 0)
5765
{
58-
Console.Error.WriteLine($"Unable to run readlink -f {dotnetPath}");
66+
output.WriteLine($"Unable to run readlink -f {dotnetPath}");
5967
Assert.True(false);
6068
}
6169
dotnetPath = dotnetPath.Trim();
6270

6371
string searchRoot = new FileInfo(dotnetPath).DirectoryName;
6472
var searchRootDirectory = new System.IO.DirectoryInfo(searchRoot);
6573

66-
Console.WriteLine($"Searching for dotnet binaries in {searchRoot}");
74+
output.WriteLine($"Searching for dotnet binaries in {searchRoot}");
6775

6876
var architecture = RuntimeInformation.OSArchitecture;
6977
var machine = GetCurrentMachine(architecture);
@@ -106,11 +114,11 @@ public void ValidateAssemblies()
106114

107115
if (valid)
108116
{
109-
Console.WriteLine($"{assembly}: OK");
117+
output.WriteLine($"{assembly}: OK");
110118
}
111119
else
112120
{
113-
Console.WriteLine($"error: {assembly} hasMethods: {hasMethods}, hasAot: {hasAot}, inReleaseMode: {inReleaseMode}");
121+
output.WriteLine($"error: {assembly} hasMethods: {hasMethods}, hasAot: {hasAot}, inReleaseMode: {inReleaseMode}");
114122
allOkay = false;
115123
}
116124
}

version-apis/VersionTest.cs

+12-4
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,34 @@
33
using System.Runtime.InteropServices;
44
using System.Text.RegularExpressions;
55
using Xunit;
6+
using Xunit.Abstractions;
67

78
namespace DotNetCoreVersionApis
89
{
910
public class VersionTest
1011
{
1112
public static readonly int MAX_DOTNET_MAJOR_VERSION = 10;
1213

14+
private readonly ITestOutputHelper output;
15+
16+
public VersionTest(ITestOutputHelper output)
17+
{
18+
this.output = output;
19+
}
20+
1321
[Fact]
1422
public void EnvironmentVersion()
1523
{
1624
var version = Environment.Version;
17-
Console.WriteLine($"Environment.Version: {version}");
25+
output.WriteLine($"Environment.Version: {version}");
1826
Assert.InRange(version.Major, 3, MAX_DOTNET_MAJOR_VERSION);
1927
}
2028

2129
[Fact]
2230
public void RuntimeInformationFrameworkDescription()
2331
{
2432
var description = RuntimeInformation.FrameworkDescription;
25-
Console.WriteLine($"RuntimeInformation.FrameworkDescription: {description}");
33+
output.WriteLine($"RuntimeInformation.FrameworkDescription: {description}");
2634
Assert.StartsWith(".NET", description);
2735
}
2836

@@ -31,11 +39,11 @@ public void RuntimeInformationFrameworkDescription()
3139
[InlineData("corefx", typeof(Uri))]
3240
public void CommitHashesAreAvailable(string repo, Type type)
3341
{
34-
Console.WriteLine($"Testing commit hashes for {repo}");
42+
output.WriteLine($"Testing commit hashes for {repo}");
3543

3644
var attributes = (AssemblyInformationalVersionAttribute[])type.Assembly.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute),false);
3745
var versionAttribute = attributes[0];
38-
Console.WriteLine($"AssemblyInformationVersionAttribute: {versionAttribute.InformationalVersion}");
46+
output.WriteLine($"AssemblyInformationVersionAttribute: {versionAttribute.InformationalVersion}");
3947

4048
string[] versionParts = versionAttribute.InformationalVersion.Split("+");
4149
Assert.Equal(2, versionParts.Length);

0 commit comments

Comments
 (0)