From dotnet-unity-tests-plugin
Use this skill when the user is working with .NET Framework 4.7.2 test projects, mentions MSTest, asks to create MSTest tests, or requests migration from MSTest v2 to MSTest v3. Also activates for keywords: "legado", "legacy .NET", "framework 4.7", "migrate MSTest", "atualizar MSTest", "MSTest v3", "projeto legado", "testes framework".
How this skill is triggered — by the user, by Claude, or both
Slash command
/dotnet-unity-tests-plugin:dotnet-mstestThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
> Esta skill cobre projetos com `<TargetFramework>` igual a `net472` (`.NET Framework 4.7.2`).
Esta skill cobre projetos com
<TargetFramework>igual anet472(.NET Framework 4.7.2). Para projetos.NET 8 / 9 / 10+use a skill dotnet-xunit.
Quando esta skill for acionada automaticamente por palavras-chave do usuário (ex.: "migrar MSTest", "atualizar MSTest", "testes projeto legado"), o assistant deve invocar o agente dotnet-tester-coordinator como ponto de entrada — ele orquestra o fluxo completo reviewer → planner → creator(s) e gerencia o diretório de sessão .dotnet-unity-tests/<session-id>/ na raiz da solução.
Consulte este documento diretamente apenas para referência pontual de padrões de código (escrever um [TestMethod] ad-hoc, tirar dúvida sobre breaking change v2→v3, lembrar versão de pacote). Para qualquer tarefa end-to-end de planejar + criar testes em uma solução, use o coordinator.
| Cenário | Framework recomendado |
|---|---|
| Projeto novo em .NET 8 / 9 / 10+ | xUnit.net (skill dotnet-xunit) |
| Manutenção em .NET Framework 4.7.2 | MSTest v3 (esta skill) |
| Ambiente 100% Microsoft (VS + ADO) | MSTest v3 (esta skill) |
| Migração de Java/JUnit para .NET moderno | NUnit → xUnit |
| Pacote | Versão | Finalidade |
|---|---|---|
MSTest.TestFramework | 3.x | Framework principal |
MSTest.TestAdapter | 3.x | Runner / integração dotnet test |
Microsoft.NET.Test.Sdk | 17.x | SDK de execução de testes |
Nota:
MSTest.Sdk(meta-package) não deve ser usado em projetos.NET Framework 4.7.2pois exige SDK-style project format completo. Use os pacotes individuais acima.
.csproj mínimo para projeto de testes<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest.TestFramework" Version="3.*" />
<PackageReference Include="MSTest.TestAdapter" Version="3.*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.*" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\<AssemblyUnderTest>\<AssemblyUnderTest>.csproj" />
</ItemGroup>
</Project>
| Elemento | Padrão | Exemplo |
|---|---|---|
| Projeto | <Assembly>.Tests.csproj | OrderService.Tests.csproj |
| Namespace | <Assembly>.Tests | OrderService.Tests |
| Classe | [TestClass] <ClassUnderTest>Tests | OrderProcessorTests |
| Método | [TestMethod] <Method>_<Scenario>_<ExpectedResult> | Process_ValidOrder_ReturnsConfirmation |
[TestClass]
public class CalculatorTests
{
[TestMethod]
public void Calculate_PositiveNumbers_ReturnsSum()
{
// Arrange
var calculator = new Calculator();
// Act
var result = calculator.Calculate(2, 3);
// Assert
Assert.AreEqual(5, result);
}
}
[TestClass]
public class CalculatorTests
{
[DataTestMethod]
[DataRow(2, 3, 5)]
[DataRow(-1, 1, 0)]
[DataRow(0, 0, 0)]
public void Calculate_GivenPairs_ReturnsExpectedSum(int a, int b, int expected)
{
// Arrange
var calculator = new Calculator();
// Act
var result = calculator.Calculate(a, b);
// Assert
Assert.AreEqual(expected, result);
}
}
[TestClass]
public class OrderProcessorTests
{
public static IEnumerable<object[]> InvalidOrders
{
get
{
yield return new object[] { null, "Order cannot be null" };
yield return new object[] { new Order { Amount = -1 }, "Amount must be positive" };
}
}
[DataTestMethod]
[DynamicData(nameof(InvalidOrders))]
public void Process_InvalidOrder_ThrowsArgumentException(Order order, string reason)
{
// Arrange
var processor = new OrderProcessor();
// Act & Assert
Assert.ThrowsException<ArgumentException>(() => processor.Process(order), reason);
}
}
[TestClass]
public class DatabaseTests
{
private static DatabaseConnection _connection;
[ClassInitialize]
public static void ClassInit(TestContext context) // TestContext é obrigatório no v3
{
_connection = DatabaseConnection.Open("connection-string");
}
[ClassCleanup]
public static void ClassCleanup()
{
_connection?.Close();
}
[TestMethod]
public void Query_ValidSql_ReturnsResults()
{
var results = _connection.Query("SELECT 1");
Assert.IsNotNull(results);
}
}
[TestClass]
public class TestAssemblySetup
{
[AssemblyInitialize]
public static void AssemblyInit(TestContext context)
{
// Executado uma vez antes de todos os testes da assembly
}
[AssemblyCleanup]
public static void AssemblyCleanup()
{
// Executado uma vez após todos os testes da assembly
}
}
[TestClass]
public class PaymentServiceTests
{
private PaymentService _service;
[TestInitialize]
public void TestInit()
{
_service = new PaymentService();
}
[TestCleanup]
public void TestCleanup()
{
_service?.Dispose();
}
[TestMethod]
public void Process_ValidPayment_ReturnsReceipt()
{
var receipt = _service.Process(new Payment { Amount = 100 });
Assert.IsNotNull(receipt);
}
}
// Em AssemblyInfo.cs ou no arquivo de setup da assembly
[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
Workers = 0usa o número de CPUs disponíveis.Scope = ExecutionScope.ClassLevelparaleliza por classe;MethodLevelpor método.
Localizar todos os projetos de teste com MSTest:
# Encontrar projetos com referência ao MSTest
grep -rl "MSTest.TestFramework" --include="*.csproj" .
# Ver versão atual dos pacotes
grep -A1 "MSTest" **/*.csproj
Usando as ferramentas do Claude:
**/*.csproj e Grep por MSTest.TestFramework2.x confirma que é v2)[ClassInitialize] sem parâmetro TestContext (breaking change)[DataRow] em métodos sem [DataTestMethod] (breaking change)Atualizar cada .csproj afetado:
<!-- DE (MSTest v2) -->
<PackageReference Include="MSTest.TestFramework" Version="2.*" />
<PackageReference Include="MSTest.TestAdapter" Version="2.*" />
<!-- PARA (MSTest v3) -->
<PackageReference Include="MSTest.TestFramework" Version="3.*" />
<PackageReference Include="MSTest.TestAdapter" Version="3.*" />
Executar restore para validar:
dotnet restore
| Mudança | Comportamento v2 | Comportamento v3 | Ação necessária |
|---|---|---|---|
[DataRow] sem [DataTestMethod] | Ignorado silenciosamente | Erro de compilação | Trocar [TestMethod] por [DataTestMethod] |
[ClassInitialize] sem TestContext | Permitido | Obrigatório | Adicionar TestContext context como parâmetro |
Assert.ThrowsException assíncrono | Limitado / workaround | Assert.ThrowsExceptionAsync<T> | Migrar para versão async |
| Nullable annotations | Sem suporte | Totalmente anotado | Revisar warnings nullable |
Corrigir [ClassInitialize] sem TestContext:
// v2 — permitido mas não recomendado
[ClassInitialize]
public static void ClassInit() { }
// v3 — parâmetro TestContext obrigatório
[ClassInitialize]
public static void ClassInit(TestContext context) { }
Corrigir [DataRow] em [TestMethod]:
// v2 — [DataRow] com [TestMethod] era ignorado silenciosamente
[TestMethod]
[DataRow(1, 2)]
public void Add_TwoNumbers_ReturnsSum(int a, int b) { }
// v3 — deve usar [DataTestMethod]
[DataTestMethod]
[DataRow(1, 2)]
public void Add_TwoNumbers_ReturnsSum(int a, int b) { }
Migrar Assert.ThrowsException assíncrono:
// v2 — workaround comum
Assert.ThrowsException<Exception>(() => asyncMethod().GetAwaiter().GetResult());
// v3 — forma correta
await Assert.ThrowsExceptionAsync<Exception>(() => asyncMethod());
Criar ou atualizar AssemblyInfo.cs no projeto de testes:
using Microsoft.VisualStudio.TestTools.UnitTesting;
[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
Identificar testes com estado compartilhado que não devem rodar em paralelo:
[TestClass]
[DoNotParallelize] // Protege classe inteira
public class StatefulIntegrationTests { }
// Ou por método individual:
[TestMethod]
[DoNotParallelize]
public void Test_WithSharedResource() { }
# Executar todos os testes
dotnet test --logger "console;verbosity=normal"
# Verificar sem warnings de obsolescência
dotnet build --warnaserror
# Comparar contagem de testes (deve ser igual antes e depois)
dotnet test --logger "trx;LogFileName=results.trx"
Checklist de validação:
[Obsolete] relacionados ao MSTest| Anti-pattern | Por quê é problema |
|---|---|
[Theory] / [InlineData] | São atributos do xUnit — não existem no MSTest |
[SetUp] / [TearDown] | São atributos do NUnit — use [TestInitialize] / [TestCleanup] |
MSTest.Sdk em projetos .NET Framework 4.7.2 | Meta-package exige SDK-style project; incompatível com FW |
[DataRow] com [TestMethod] (v3) | Gera erro de compilação; use [DataTestMethod] |
Herança entre classes [TestClass] | MSTest não suporta herança de atributos de teste de forma confiável |
| Estado estático mutável em testes paralelos | Race conditions; use [DoNotParallelize] onde necessário |
Provides a checklist for code reviews covering functionality, security, performance, maintainability, tests, and quality. Use for pull requests, audits, team standards, and developer training.
npx claudepluginhub arquitetomovel/markv-ai-plugins --plugin dotnet-unity-tests-plugin