Browse Source

Support for NUnit parametrised tests Attributes

http://nunit.org/index.php?p=parameterizedTests&r=2.6.1
pull/67/head
Dragan 13 years ago
parent
commit
622acd262b
  1. 27
      src/AddIns/Analysis/UnitTesting/Src/NUnitTestFramework.cs
  2. 9
      src/AddIns/Analysis/UnitTesting/Src/TestResultsReader.cs
  3. 91
      src/AddIns/Analysis/UnitTesting/Test/Frameworks/NUnitTestFrameworkIsTestMemberTests.cs

27
src/AddIns/Analysis/UnitTesting/Src/NUnitTestFramework.cs

@ -99,6 +99,17 @@ namespace ICSharpCode.UnitTesting
return @class.Methods.Where(IsTestMethod).Select(member => new TestMember(member)); return @class.Methods.Where(IsTestMethod).Select(member => new TestMember(member));
} }
// Single static table for all NUnit parametrised test MethodAttributes and ParameterAttributes
// Assumption is that NUnit test class will never compile with wrong attributes
static HashSet<string> ParametrisedTestAttributesSet = new HashSet<string> ( new List<string> {
"NUnit.Framework.TestCaseAttribute",
"NUnit.Framework.RandomAttribute",
"NUnit.Framework.RangeAttribute",
"NUnit.Framework.ValuesAttribute",
"NUnit.Framework.TestCaseSourceAttribute",
"NUnit.Framework.ValueSourceAttribute"
} );
static bool IsTestMethod(IMethod method) static bool IsTestMethod(IMethod method)
{ {
var nameComparer = GetNameComparer(method.DeclaringType); var nameComparer = GetNameComparer(method.DeclaringType);
@ -109,6 +120,22 @@ namespace ICSharpCode.UnitTesting
if (method.Parameters.Count == 0) { if (method.Parameters.Count == 0) {
return true; return true;
} }
// Check if Parameter is attributed with NUnit parametrised-test-attribute
else {
foreach ( var parameter in method.Parameters )
{
foreach ( var parameterAttribute in parameter.Attributes )
{
if ( ParametrisedTestAttributesSet.Contains ( parameterAttribute.AttributeType.FullyQualifiedName ) ) {
return true;
}
}
}
}
}
// Check if Method is attributed with NUnit parametrised-test-attribute
else if ( ParametrisedTestAttributesSet.Contains ( attribute.AttributeType.FullyQualifiedName ) ){
return true;
} }
} }
} }

9
src/AddIns/Analysis/UnitTesting/Src/TestResultsReader.cs

@ -126,6 +126,15 @@ namespace ICSharpCode.UnitTesting
{ {
string name = nameBuilder.ToString(); string name = nameBuilder.ToString();
if (name == "Name") { if (name == "Name") {
// Remove arguments from valueBuilder result to match result name with test method name
int leftDelimiterIndex = valueBuilder.ToString().IndexOf('(');
if ( leftDelimiterIndex > 0 ) {
int rightDelimiterIndex = valueBuilder.ToString().IndexOf(')',leftDelimiterIndex);
if ( rightDelimiterIndex > leftDelimiterIndex
&& rightDelimiterIndex == valueBuilder.Length - 1 ) {
valueBuilder.Remove( leftDelimiterIndex, rightDelimiterIndex - leftDelimiterIndex + 1 );
}
}
result = new TestResult(valueBuilder.ToString()); result = new TestResult(valueBuilder.ToString());
} else if (result != null) { } else if (result != null) {
if (name == "Message") { if (name == "Message") {

91
src/AddIns/Analysis/UnitTesting/Test/Frameworks/NUnitTestFrameworkIsTestMemberTests.cs

@ -134,7 +134,7 @@ namespace UnitTesting.Tests.Frameworks
Assert.IsFalse(result); Assert.IsFalse(result);
} }
[Test] [Test]
public void IsTestMember_FieldHasOneAttribute_ReturnsFalseAndDoesNotThrowInvalidCastException() public void IsTestMember_FieldHasOneAttribute_ReturnsFalseAndDoesNotThrowInvalidCastException()
{ {
@ -148,5 +148,94 @@ namespace UnitTesting.Tests.Frameworks
Assert.IsFalse(result); Assert.IsFalse(result);
} }
[Test]
public void IsTestMember_MethodHasParametersAndTestCaseAttribute_ReturnsTrue()
{
CreateTestFramework();
var testAttribute = new MockAttribute("NUnit.Framework.TestCaseAttribute");
MockMethod mockMethod = MockMethod.CreateMockMethodWithAttribute(testAttribute);
var mockParameter = new MockParameter();
mockMethod.Parameters.Add(mockParameter);
bool result = testFramework.IsTestMember(mockMethod);
Assert.IsTrue(result);
}
[Test]
public void IsTestMember_MethodHasParametersAndTestCaseSourceAttribute_ReturnsTrue()
{
CreateTestFramework();
var mockMethodAttribute = new MockAttribute("NUnit.Framework.TestCaseSourceAttribute");
MockMethod mockMethod = MockMethod.CreateMockMethodWithAttribute(mockMethodAttribute);
var mockParameter = new MockParameter();
mockMethod.Parameters.Add(mockParameter);
bool result = testFramework.IsTestMember(mockMethod);
Assert.IsTrue(result);
}
[Test]
public void IsTestMember_MethodHasParametersAndTestAttribute_ParameterHasRandomAttribute_ReturnsTrue()
{
CreateTestFramework();
MockMethod mockMethod = MockMethod.CreateMockMethodWithAttribute(new MockAttribute("Test"));
var mockParameter = new MockParameter();
var mockParamAttribute = new MockAttribute("NUnit.Framework.RandomAttribute");
mockParameter.Attributes.Add(mockParamAttribute);
mockMethod.Parameters.Add(mockParameter);
bool result = testFramework.IsTestMember(mockMethod);
Assert.IsTrue(result);
}
[Test]
public void IsTestMember_MethodHasParametersAndTestAttribute_ParameterHasRangeAttribute_ReturnsTrue()
{
CreateTestFramework();
MockMethod mockMethod = MockMethod.CreateMockMethodWithAttribute(new MockAttribute("Test"));
var mockParameter = new MockParameter();
var mockParamAttribute = new MockAttribute("NUnit.Framework.RangeAttribute");
mockParameter.Attributes.Add(mockParamAttribute);
mockMethod.Parameters.Add(mockParameter);
bool result = testFramework.IsTestMember(mockMethod);
Assert.IsTrue(result);
}
[Test]
public void IsTestMember_MethodHasParametersAndTestAttribute_ParameterHasValuesAttribute_ReturnsTrue()
{
CreateTestFramework();
MockMethod mockMethod = MockMethod.CreateMockMethodWithAttribute(new MockAttribute("Test"));
var mockParameter = new MockParameter();
var mockParamAttribute = new MockAttribute("NUnit.Framework.ValuesAttribute");
mockParameter.Attributes.Add(mockParamAttribute);
mockMethod.Parameters.Add(mockParameter);
bool result = testFramework.IsTestMember(mockMethod);
Assert.IsTrue(result);
}
[Test]
public void IsTestMember_MethodHasParametersAndTestAttribute_ParameterHasValueSourceAttribute_ReturnsTrue()
{
CreateTestFramework();
MockMethod mockMethod = MockMethod.CreateMockMethodWithAttribute(new MockAttribute("Test"));
var mockParameter = new MockParameter();
var mockParamAttribute = new MockAttribute("NUnit.Framework.ValueSourceAttribute");
mockParameter.Attributes.Add(mockParamAttribute);
mockMethod.Parameters.Add(mockParameter);
bool result = testFramework.IsTestMember(mockMethod);
Assert.IsTrue(result);
}
} }
} }

Loading…
Cancel
Save