Nel mio caso vorrei caricare dati da un file CSV, ma non potevo passare il nome del file alla "fonte dati". Dopo aver faticato un po ', vengo a questa soluzione da due soldi.
In un primo momento ho ereditato TestCaseSourceAttirbute
Poi ho creato lo strato di dati, nel mio caso un lettore CSV.
/// <summary>
/// Test data provider
/// </summary>
/// <typeparam name="T">Type to return in enumerable</typeparam>
/// <typeparam name="C">Configuration type that provide Filenames</typeparam>
public sealed class TestCsvReader<T, C>
{
/// <summary>
/// Initializes a new instance of the <see cref="TestCsvReader{T, C}"/> class.
/// </summary>
public TestCsvReader()
{
this.Config = (C)Activator.CreateInstance<C>();
}
/// <summary>
/// Gets or sets the configuration.
/// </summary>
/// <value>
/// The configuration.
/// </value>
private C Config { get; set; }
/// <summary>
/// Gets the filename.
/// </summary>
/// <value>
/// The filename.
/// </value>
/// <exception cref="System.Exception">
/// </exception>
private string Filename
{
get
{
try
{
string result = Convert.ToString(typeof(C).GetProperty(string.Format("{0}Filename", typeof(T).Name)).GetValue(this.Config));
if (!File.Exists(result))
throw new Exception(string.Format("Unable to find file '{0}' specified in property '{1}Filename' in class '{1}'", result, typeof(C).Name));
return result;
}
catch
{
throw new Exception(string.Format("Unable to find property '{0}Filename' in class '{1}'", typeof(T).Name, typeof(C).Name));
}
}
}
/// <summary>
/// Yields values from source
/// </summary>
/// <returns></returns>
public IEnumerable Data()
{
string file = this.Filename;
T[] result = null;
using (StreamReader reader = File.OpenText(file))
{
//TODO: do it here your magic
}
yield return new TestCaseData(result);
}
}
Quindi ho creato una classe con l'unico ambito per contenere le proprietà con i percorsi dei file. C'è una convenzione sul nome della proprietà, che è ClassTypeName + "Filename".
public class Configurations
{
public string ConflictDataFilename
{
get
{
return @"C:\test.csv";
}
}
}
A questo punto basta decorare di conseguenza il test, con il tipo di classe per mappare i dati e la classe che contiene il percorso del file.
[Test(Description="Try this one")]
[TestCaseCsv(typeof(ClassMappedToData), typeof(Configurations))]
public void Infinite(ClassMappedToData[] data)
{
}
Spero che questo può aiutare un po '.
fonte
2015-06-11 11:45:06
http://stackoverflow.com/questions/8869734/how-to-use-testcase-in-nunit-2-5 –
Non è quello che stavo cercando! – sparkr