Hai taggato la tua domanda con NUnit
, ma chiedi informazioni su MSTest
. Ciò che state chiedendo può essere ottenuto con proiettori parametrizzati in NUnit. Non ho familiarità con MSTest per suggerire un approccio equivalente lì, e una ricerca rapida indica che MSTest potrebbe non avere questa funzione.
In NUnit parametrizzare il dispositivo di prova applicando più attributi [TestFixture(...)]
alla classe di dispositivo con parametri diversi. Questi parametri verranno passati al costruttore della fixture.
Poiché esistono limiti sui tipi di parametro che è possibile passare, è necessario passare una stringa specificando l'algoritmo, quindi nel costruttore assegnare il delegato o l'oggetto che fornisce l'algoritmo di ricerca a un campo membro che viene utilizzato nei test.
Ad esempio:
using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace MyTests
{
public static class SearchAlgorithms
{
public static int DefaultSearch(int target, IList<int> data)
{
return data.IndexOf(target);
}
public static int BrokenSearch(int target, IList<int> data)
{
return 789;
}
}
[TestFixture("forward")]
[TestFixture("broken")]
public class SearchTests
{
private Func<int, IList<int>, int> searchMethod;
public SearchTests(string algorithmName)
{
if (algorithmName == "forward")
{
this.searchMethod = SearchAlgorithms.DefaultSearch;
return;
}
if (algorithmName == "broken")
{
this.searchMethod = SearchAlgorithms.BrokenSearch;
}
}
[Test]
public void SearchFindsCorrectIndex()
{
Assert.AreEqual(
1, this.searchMethod(2, new List<int> { 1, 2, 3 }));
}
[Test]
public void SearchReturnsMinusOneWhenTargetNotPresent()
{
Assert.AreEqual(
-1, this.searchMethod(4, new List<int> { 1, 2, 3 }));
}
}
}
Nella tua domanda chiedi informazioni su MSTest, ma nei tuoi tag specifichi NUnit. Per quale risposta vuoi? –