Skip to content

Commit

Permalink
Added some sample xUnit tests to publish.
Browse files Browse the repository at this point in the history
  • Loading branch information
shane-agoda committed Jun 20, 2023
1 parent ecba50b commit 4fc18f3
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Agoda.Tests.Metrics.xUnit\Agoda.Tests.Metrics.xUnit.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly: Xunit.TestFramework("Agoda.Tests.Metrics.xUnit.AgodaTestFramework", "Agoda.Tests.Metrics.xUnit")]
35 changes: 35 additions & 0 deletions src/Agoda.Tests.Metrics.xUnit.Tests/TestSuite1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Agoda.Tests.Metrics.xUnit.Tests
{
public class TestSuite1
{
private readonly Random _random = new Random();

[Fact]
public void Test1()
{
// 25% failure rate
Assert.True(_random.Next(0, 100) > 25);
}

[Fact]
public void Test2()
{
// 25% failure rate
Assert.True(_random.Next(0, 100) > 25);
}

[Fact]
public void Test3()
{
// 25% failure rate
Assert.True(_random.Next(0, 100) > 25);
}

[Fact]
public void Test4()
{
// 25% failure rate
Assert.True(_random.Next(0, 100) > 25);
}
}
}
45 changes: 45 additions & 0 deletions src/Agoda.Tests.Metrics.xUnit.Tests/TestSuite2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Agoda.Tests.Metrics.xUnit.Tests
{
public class TestSuite2
{
private readonly Random _random = new Random();

[Fact]
public void Test1()
{
// 25% failure rate
Assert.True(_random.Next(0, 100) > 25);
}

[Fact]
public void Test2()
{
// 25% failure rate
Assert.True(_random.Next(0, 100) > 25);
}

[Fact]
public void Test3()
{
// 25% failure rate
Assert.True(_random.Next(0, 100) > 25);
}

[Fact]
public void Test4()
{
// 25% failure rate
Assert.True(_random.Next(0, 100) > 25);
}

[Theory]
[InlineData(100)]
[InlineData(75)]
[InlineData(50)]
[InlineData(25)]
public void Test5(int n)
{
Assert.True(n > 25);
}
}
}
1 change: 1 addition & 0 deletions src/Agoda.Tests.Metrics.xUnit.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,4 @@
<ProjectReference Include="..\Agoda.Tests.Metrics\Agoda.Tests.Metrics.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/Agoda.Tests.Metrics.xUnit/AgodaExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected override async void RunTestCases(IEnumerable<IXunitTestCase> testCases
var assemblyRunner = new AgodaAssemblyRunner(TestAssembly, testCases, DiagnosticMessageSink, executionMessageSink, executionOptions, _builder);
await assemblyRunner.RunAsync();
_builder.Publish();
_messageSink.OnMessage(new DiagnosticMessage("Published test results"));
}
catch (Exception ex)
{
Expand Down
18 changes: 11 additions & 7 deletions src/Agoda.Tests.Metrics.xUnit/TestResultsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ public TestResultsBuilder()
/// </summary>
public void Publish()
{
// Create the payload
var payload = new TestCasePayload(
typeof(TestResultsBuilder).Assembly.GetName().Version.ToString(),
GitContextReader.GetGitContext(),
_testResults
);
DevFeedbackPublisher.Publish(null, payload, DevLocalDataType.NUnit);
lock (BuilderLock)
{
// Create the payload
var payload = new TestCasePayload(
typeof(TestResultsBuilder).Assembly.GetName().Version.ToString(),
GitContextReader.GetGitContext(),
_testResults
);
// Publish it
DevFeedbackPublisher.Publish(null, payload, DevLocalDataType.NUnit);
}
}

/// <summary>
Expand Down
4 changes: 0 additions & 4 deletions src/Agoda.Tests.Metrics.xUnit/xunit.runner.json

This file was deleted.

8 changes: 7 additions & 1 deletion src/DotnetBuildMetrics.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Agoda.Tests.Metrics", "Agod
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Agoda.Tests.Metrics.xUnit", "Agoda.Tests.Metrics.xUnit\Agoda.Tests.Metrics.xUnit.csproj", "{449942D5-1D87-42A9-A74F-0BA5FB9FAC97}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Agoda.Tests.Metrics.Tests", "Agoda.Tests.Metrics.Tests\Agoda.Tests.Metrics.Tests.csproj", "{80645931-22CF-4A46-B965-544E7B5D7855}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Agoda.Tests.Metrics.Tests", "Agoda.Tests.Metrics.Tests\Agoda.Tests.Metrics.Tests.csproj", "{80645931-22CF-4A46-B965-544E7B5D7855}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Agoda.Tests.Metrics.xUnit.Tests", "Agoda.Tests.MEtrics.xUnit.Tests\Agoda.Tests.Metrics.xUnit.Tests.csproj", "{66B8CAFD-5EB6-4FF4-B2F5-6D9CE0A71396}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -57,6 +59,10 @@ Global
{80645931-22CF-4A46-B965-544E7B5D7855}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80645931-22CF-4A46-B965-544E7B5D7855}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80645931-22CF-4A46-B965-544E7B5D7855}.Release|Any CPU.Build.0 = Release|Any CPU
{66B8CAFD-5EB6-4FF4-B2F5-6D9CE0A71396}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{66B8CAFD-5EB6-4FF4-B2F5-6D9CE0A71396}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66B8CAFD-5EB6-4FF4-B2F5-6D9CE0A71396}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66B8CAFD-5EB6-4FF4-B2F5-6D9CE0A71396}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 4fc18f3

Please sign in to comment.