Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DeviceRunners.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<Folder Name="/sample/test/">
<Project Path="sample/test/DeviceTestingKitApp.DeviceTests/DeviceTestingKitApp.DeviceTests.csproj" />
<Project Path="sample/test/DeviceTestingKitApp.Library.NUnitTests/DeviceTestingKitApp.Library.NUnitTests.csproj" />
<Project Path="sample/test/DeviceTestingKitApp.Library.MSTestTests/DeviceTestingKitApp.Library.MSTestTests.csproj" />
<Project Path="sample/test/DeviceTestingKitApp.MauiLibrary.XunitTests/DeviceTestingKitApp.MauiLibrary.XunitTests.csproj" />
<Project Path="sample/test/DeviceTestingKitApp.MauiLibrary.Xunit3Tests/DeviceTestingKitApp.MauiLibrary.Xunit3Tests.csproj" />
<Project Path="sample/test/DeviceTestingKitApp.BrowserTests/DeviceTestingKitApp.BrowserTests.csproj" />
Expand All @@ -27,6 +28,7 @@
<Project Path="src/DeviceRunners.UITesting/DeviceRunners.UITesting.csproj" />
<Project Path="src/DeviceRunners.VisualRunners.Core/DeviceRunners.VisualRunners.Core.csproj" />
<Project Path="src/DeviceRunners.VisualRunners.Maui/DeviceRunners.VisualRunners.Maui.csproj" />
<Project Path="src/DeviceRunners.VisualRunners.MSTest/DeviceRunners.VisualRunners.MSTest.csproj" />
<Project Path="src/DeviceRunners.VisualRunners.NUnit/DeviceRunners.VisualRunners.NUnit.csproj" />
<Project Path="src/DeviceRunners.VisualRunners.Xunit/DeviceRunners.VisualRunners.Xunit.csproj" />
<Project Path="src/DeviceRunners.VisualRunners.Xunit3/DeviceRunners.VisualRunners.Xunit3.csproj" />
Expand Down
4 changes: 4 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<PackageVersion Include="Microsoft.Maui.Controls" Version="10.0.80" />
<PackageVersion Include="Microsoft.Maui.Controls.Compatibility" Version="10.0.80" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.7.0" />
<PackageVersion Include="Microsoft.Testing.Platform" Version="2.4.0" />
<PackageVersion Include="Microsoft.Testing.Platform.ServerMode.Client.Sources" Version="2.4.0" />
<PackageVersion Include="MSTest.TestAdapter" Version="4.4.0" />
<PackageVersion Include="MSTest.TestFramework" Version="4.4.0" />
<PackageVersion Include="NSubstitute" Version="5.3.0" />
<PackageVersion Include="NUnit" Version="4.6.1" />
<PackageVersion Include="NUnit.Analyzers" Version="4.14.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,20 @@
<TrimmerRootAssembly Include="DeviceTestingKitApp.MauiLibrary.XunitTests" RootMode="all" />
<TrimmerRootAssembly Include="DeviceTestingKitApp.MauiLibrary.Xunit3Tests" RootMode="all" />
<TrimmerRootAssembly Include="DeviceTestingKitApp.Library.NUnitTests" RootMode="all" />
<TrimmerRootAssembly Include="DeviceTestingKitApp.Library.MSTestTests" RootMode="all" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DeviceTestingKitApp.MauiLibrary.XunitTests\DeviceTestingKitApp.MauiLibrary.XunitTests.csproj" />
<ProjectReference Include="..\DeviceTestingKitApp.MauiLibrary.Xunit3Tests\DeviceTestingKitApp.MauiLibrary.Xunit3Tests.csproj" />
<ProjectReference Include="..\DeviceTestingKitApp.Library.NUnitTests\DeviceTestingKitApp.Library.NUnitTests.csproj" />
<ProjectReference Include="..\DeviceTestingKitApp.Library.MSTestTests\DeviceTestingKitApp.Library.MSTestTests.csproj" />
<ProjectReference Include="..\..\..\src\DeviceRunners.UITesting.Xunit\DeviceRunners.UITesting.Xunit.csproj" />
<ProjectReference Include="..\..\..\src\DeviceRunners.UITesting.Maui\DeviceRunners.UITesting.Maui.csproj" />
<ProjectReference Include="..\..\..\src\DeviceRunners.VisualRunners.Xunit\DeviceRunners.VisualRunners.Xunit.csproj" />
<ProjectReference Include="..\..\..\src\DeviceRunners.VisualRunners.Xunit3\DeviceRunners.VisualRunners.Xunit3.csproj" />
<ProjectReference Include="..\..\..\src\DeviceRunners.VisualRunners.NUnit\DeviceRunners.VisualRunners.NUnit.csproj" />
<ProjectReference Include="..\..\..\src\DeviceRunners.VisualRunners.MSTest\DeviceRunners.VisualRunners.MSTest.csproj" />
<ProjectReference Include="..\..\..\src\DeviceRunners.VisualRunners.Maui\DeviceRunners.VisualRunners.Maui.csproj" />
</ItemGroup>

Expand Down
4 changes: 3 additions & 1 deletion sample/test/DeviceTestingKitApp.DeviceTests/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ public static MauiApp CreateMauiApp()
.AddTestAssemblies(typeof(DeviceTestingKitApp.MauiLibrary.XunitTests.UnitTests).Assembly)
.AddTestAssemblies(typeof(DeviceTestingKitApp.MauiLibrary.Xunit3Tests.UnitTests).Assembly)
.AddTestAssemblies(typeof(DeviceTestingKitApp.Library.NUnitTests.UnitTests).Assembly)
.AddTestAssemblies(typeof(DeviceTestingKitApp.Library.MSTestTests.UnitTests).Assembly)
.AddXunit()
.AddXunit3()
.AddNUnit());
.AddNUnit()
.AddMSTest());

#if DEBUG
builder.Logging.AddDebug();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<!-- MSTest 4.x runs on Microsoft.Testing.Platform. This library only supplies the test
attributes; the device runner loads the assembly and discovers/runs it in-process via
DeviceRunners.VisualRunners.MSTest, so no MTP runner/adapter is referenced here.
PrivateAssets blocks MSTest's buildTransitive 'global using Microsoft.VisualStudio.TestTools.UnitTesting'
from flowing to the host app (whose own tests are xunit), which would make 'Assert' ambiguous. -->
<PackageReference Include="MSTest.TestFramework" PrivateAssets="build;buildTransitive" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\DeviceTestingKitApp.Library\DeviceTestingKitApp.Library.csproj" />
</ItemGroup>

</Project>
116 changes: 116 additions & 0 deletions sample/test/DeviceTestingKitApp.Library.MSTestTests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
namespace DeviceTestingKitApp.Library.MSTestTests;

[TestClass]
public class UnitTests
{
public static IEnumerable<(int Value, string Name)> GetTestData()
{
yield return (1, "first");
yield return (2, "second");
}

public static IEnumerable<(int Value, string Name)> TestDataProperty =>
[
(1, "first"),
(2, "second")
];

public static IEnumerable<(int Value, string Name)> TestDataField =
[
(1, "first"),
(2, "second")
];

// Process-wide on purpose: MSTest's [Retry] re-runs the method in the same host, and a fresh
// class instance is created for every attempt, so instance state cannot survive between them.
static int _retryAttempts;

public TestContext TestContext { get; set; }

[TestMethod]
public void SuccessfulTest()
{
var value = true;
Assert.IsTrue(value);
}

[TestMethod]
[Ignore("This test is skipped.")]
public void SkippedTest()
{
}

[TestMethod]
[TestCategory("ExpectedFailure")]
public void FailingTest()
{
throw new Exception("This is meant to fail.");
}

[TestMethod]
[TestCategory("Retry")]
[Retry(3)]
public void FlakyTestPassesOnRetry()
{
// Fails on the first attempt and passes on the retry. Microsoft.Testing.Platform reports
// every [Retry] attempt under the same test node UID, tagging the earlier one
// retry.is-superseded; the visual runner and result channels must record only the final
// passing attempt, not the superseded failure.
var attempt = Interlocked.Increment(ref _retryAttempts);
if (attempt == 1)
Assert.Fail("Simulated flaky failure on the first attempt; the retry is expected to pass.");
}

[TestMethod]
[DataRow(1)]
public void DataRowTest(int value)
{
Assert.AreEqual(1, value);
}

[TestMethod]
[DataRow(1, 2, 3, 4)]
[DataRow(10, 20)]
public void TestWithParams(params int[] values)
{
Assert.IsTrue(values.Length > 0);
}

[TestMethod]
[DataRow(42)]
[DataRow("alpha")]
public void Value_RoundTrips<T>(T value)
{
#pragma warning disable MSTEST0032 // Assertion condition is always true
Assert.AreEqual(value, value);
#pragma warning restore MSTEST0032 // Assertion condition is always true
}

[TestMethod]
[DynamicData(nameof(GetTestData))]
public void TestWithMethod(int value, string name)
{
Assert.IsTrue(value > 0);
}

[TestMethod]
[DynamicData(nameof(TestDataProperty))]
public void TestWithProperty(int value, string name)
{
Assert.IsTrue(value > 0);
}

[TestMethod]
[DynamicData(nameof(TestDataField))]
public void TestWithField(int value, string name)
{
Assert.IsTrue(value > 0);
}

[TestMethod]
public async Task CooperativeCancellationTest()
{
Assert.IsNotNull(TestContext);
await Task.Delay(1, TestContext.CancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using DeviceTestingKitApp.ViewModels;

namespace DeviceTestingKitApp.Library.MSTestTests.ViewModels;

[TestClass]
public class CounterViewModelTests
{
[TestMethod]
public void InitialStateIsCorrect()
{
var vm = new CounterViewModel();

Assert.AreEqual(0, vm.Count);
}

[TestMethod]
public void SingleIncrementIncrementsByOne()
{
var vm = new CounterViewModel();

vm.IncrementCommand.Execute(null);

Assert.AreEqual(1, vm.Count);
}

[TestMethod]
[DataRow(1)]
[DataRow(2)]
[DataRow(3)]
public void IncrementsIncrementCorrectly(int executeCount)
{
var vm = new CounterViewModel();

for (var i = 0; i < executeCount; i++)
{
vm.IncrementCommand.Execute(null);
}

Assert.AreEqual(executeCount, vm.Count);
}

[TestMethod]
public void UpdatingCountPropertyTriggersPropertyChanged()
{
var vm = new CounterViewModel();

var eventTrigger = 0;
vm.PropertyChanged += (sender, args) =>
{
Assert.AreEqual(nameof(vm.Count), args.PropertyName);
eventTrigger++;
};

vm.Count = 5;

Assert.AreEqual(1, eventTrigger);
}

[TestMethod]
public void ExecutingCommandTriggersPropertyChanged()
{
var vm = new CounterViewModel();

var eventTrigger = 0;
vm.PropertyChanged += (sender, args) =>
{
Assert.AreEqual(nameof(vm.Count), args.PropertyName);
eventTrigger++;
};

vm.IncrementCommand.Execute(null);

Assert.AreEqual(1, eventTrigger);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net9.0;net10.0</TargetFrameworks>
<IsPackable>true</IsPackable>
<!-- This is a runner library, not a test project: do not generate an MTP entry point. -->
<EnableMSTestRunner>false</EnableMSTestRunner>
<GenerateProgramFile>false</GenerateProgramFile>
<IsTestingPlatformApplication>false</IsTestingPlatformApplication>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<!-- Microsoft.Testing.Platform provides the ITestApplication builder/host used to run the
platform in server mode in-process. MSTest.TestAdapter provides the AddMSTest(...) builder
extension that hosts the MSTest framework. PrivateAssets="compile" keeps the MSTest
attribute types from flowing transitively to consuming test libraries, which bring their
own reference. -->
<PackageReference Include="Microsoft.Testing.Platform" />
<PackageReference Include="Microsoft.Testing.Platform.ServerMode.Client.Sources">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="MSTest.TestAdapter" PrivateAssets="compile;build;buildTransitive" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DeviceRunners.VisualRunners\DeviceRunners.VisualRunners.csproj" />
</ItemGroup>

</Project>
Loading
Loading