我想在它们运行之前报告它们,并且可以选择通过shell脚本运行单个测试而无需管理类别。我们有一些非托管代码可能会使进程处于错误状态,有时很高兴每个nunit-console运行时单独运行每个测试。
答案 0 :(得分:1)
现在有--explore
命令行选项可用于列出所有测试用例而不运行测试。更具体地说
nunit3-console.exe MyProject.dll --explore
了解更多信息:https://github.com/nunit/docs/wiki/Console-Command-Line#description
答案 1 :(得分:0)
nunit-console似乎仍然不支持此选项。但是,使用反射获取测试用例列表相当简单。在最基本的级别,您需要具有相应public
属性的任何public class
的所有[Test]/[TestFixture]
方法的列表。根据测试的结构,您可能需要执行其他过滤,例如删除标有[Ignore]
属性的任何测试,或者考虑基类中的测试方法。
在基本级别,代码看起来像这样:
// Load the assembly containing your fixtures
Assembly a = Assembly.LoadFrom(assemblyName);
// Foreach public class that is a TestFixture and not Ignored
foreach (var c in a.GetTypes()
.Where(x=>x.IsPublic
&& (x.GetCustomAttributes(typeof(NUnit.Framework.TestFixtureAttribute)).Count() > 0)
&& (x.GetCustomAttributes(typeof(NUnit.Framework.IgnoreAttribute)).Count() ==0)))
{
// For each public method that is a Test and not Ignored
foreach (var m in c.GetMethods()
.Where(x=>x.IsPublic
&& (x.GetCustomAttributes(typeof(NUnit.Framework.TestAttribute)).Count() > 0)
&& (x.GetCustomAttributes(typeof(NUnit.Framework.IgnoreAttribute)).Count() ==0)))
{
// Print out the test name
Console.WriteLine("{0}.{1}", c.ToString(), m.Name);
// Alternately, print out the command line to run test case using nunit-console
//Console.WriteLine("nunit-console /run:{0}.{1} {2}", c.ToString(), m.Name, assemblyName);
}
}
如果您只想要特定TestFixture
的测试方法,显然您可以简化这一点。
正如评论中所说,如果您需要关注其他NUnit属性,例如TestCase
和TestCaseSource
,这会更复杂一些。我修改了下面的代码以支持这些属性的一些功能。
static void PrintTestNames(string assemblyName) {
Assembly assembly = Assembly.LoadFrom(assemblyName);
foreach (var fixture in assembly.GetTypes().Where(x => x.IsPublic
&& (x.GetCustomAttributes(typeof(TestFixtureAttribute)).Count() > 0)
&& (x.GetCustomAttributes(typeof(IgnoreAttribute)).Count() == 0))) {
foreach(var method in fixture.GetMethods().Where(x=>x.IsPublic
&& (x.GetCustomAttributes(typeof(IgnoreAttribute)).Count() == 0)
&& ((x.GetCustomAttributes(typeof(TestAttribute)).Count() > 0)
|| (x.GetCustomAttributes(typeof(TestCaseAttribute)).Count() > 0)
|| (x.GetCustomAttributes(typeof(TestCaseSourceAttribute)).Count() > 0))
)) {
var testAttributes = method.GetCustomAttributes(typeof(TestAttribute)) as IEnumerable<TestAttribute>;
var caseAttributes = method.GetCustomAttributes(typeof(TestCaseAttribute)) as IEnumerable<TestCaseAttribute>;
var caseSourceAttributes = method.GetCustomAttributes(typeof(TestCaseSourceAttribute)) as IEnumerable<TestCaseSourceAttribute>;
if (caseAttributes.Count() > 0) {
foreach(var testCase in caseAttributes) {
if (!string.IsNullOrEmpty(testCase.TestName)) {
PrintTestName(fixture.ToString(), testCase.TestName);
}
else {
string arguments = ExtractArguments(testCase.Arguments);
PrintTestName(fixture.ToString(), method.Name + arguments);
}
}
}
else if (caseSourceAttributes.Count() > 0) {
foreach (var testCase in caseSourceAttributes) {
var sourceName = testCase.SourceName;
if (!string.IsNullOrEmpty(sourceName)) {
var staticMember = fixture.GetField(sourceName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
var instanceMember = fixture.GetField(sourceName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
var staticMethodMember = fixture.GetMethod(sourceName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
var instanceMethodMember = fixture.GetMethod(sourceName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
var staticPropMember = fixture.GetProperty(sourceName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
var instancePropMember = fixture.GetProperty(sourceName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
IEnumerable memberValues;
if (null != staticMember) {
memberValues = staticMember.GetValue(null) as IEnumerable;
}
else if (null != instanceMember) {
var instance = Activator.CreateInstance(fixture);
memberValues = instanceMember.GetValue(instance) as IEnumerable;
} else if(null != staticMethodMember) {
memberValues = staticMethodMember.Invoke(null,new object [0]) as IEnumerable;
}
else if (null != instanceMethodMember) {
var instance = Activator.CreateInstance(fixture);
memberValues = instanceMethodMember.Invoke(instance, new object[0]) as IEnumerable;
}
else if (null != staticPropMember) {
memberValues = staticPropMember.GetValue(null) as IEnumerable;
}
else if (null != instancePropMember) {
var instance = Activator.CreateInstance(fixture);
memberValues = instancePropMember.GetValue(instance) as IEnumerable;
}
else {
Console.WriteLine("*** Ooops...Looks like I don't know how to get {0} for fixture {1}", sourceName, fixture.ToString());
continue;
}
foreach (var memberValue in memberValues) {
if (null != memberValue as IEnumerable) {
PrintTestName(fixture.ToString(), method.Name + ExtractArguments(memberValue as IEnumerable));
}
else {
PrintTestName(fixture.ToString(), method.Name + "(" + memberValue.ToString() + ")");
}
}
} else {
Console.WriteLine("*** Ooops...Looks like I don't know how to handle test {0} for fixture {1}", method.Name, fixture.ToString());
}
}
}
else {
PrintTestName(fixture.ToString(), method.Name);
}
}
}
}
static string ExtractArguments(IEnumerable arguments) {
string caseArgs = "(";
bool first = true;
foreach (var arg in arguments) {
if (first) first = false;
else caseArgs += ",";
caseArgs += Convert.ToString(arg);
}
return caseArgs + ")";
}
static void PrintTestName(string fixture, string testName) {
Console.WriteLine("{0}.{1}", fixture, testName);
//Console.WriteLine("nunit-console /run:{0}.{1} {2}", fixture, testName, assemblyName);
}
如果查看上面的代码,您可能会注意到我已经处理了TestCaseSource
测试是命名属性/方法/字段的字符串的功能。您还会注意到,虽然有更多代码,但它仍然是非常简单的代码,因此如果您使用的是替代版本的TestCaseSource,或者如果您有其他NUnit属性,则可以轻松扩展我正在使用我没有照顾过的。
在上面添加一个计数器也很容易,这样你就可以获得与nunit-console运行的测试次数相同数量的测试的舒适度。