public override void UpdateTestResult(TestResult result) { int lastDot = result.Name.LastIndexOf('.'); if (lastDot < 0) { return; } string fixtureName = result.Name.Substring(0, lastDot); string methodName = result.Name.Substring(lastDot + 1); NUnitTestClass testClass = GetTestClass(new FullTypeName(fixtureName)); if (testClass == null) { // maybe it's an inherited test int secondToLastDot = result.Name.LastIndexOf('.', lastDot - 1); if (secondToLastDot >= 0) { string fixtureName2 = result.Name.Substring(0, secondToLastDot); methodName = result.Name.Substring(secondToLastDot + 1); testClass = GetTestClass(new FullTypeName(fixtureName2)); } } if (testClass != null) { NUnitTestMethod testMethod = testClass.FindTestMethod(methodName); if (testMethod != null) { testMethod.UpdateTestResult(result); } } }
public override void SetUp() { base.SetUp(); AddCodeFile("test.cs", @"using NUnit.Framework; namespace RootNamespace.Tests { [TestFixture] class MyTestFixture { [Test] public void TestMethod1() {} [Test] public void TestMethod2() {} } }"); testClass = testProject.GetTestClass(new FullTypeName("RootNamespace.Tests.MyTestFixture")); testMethod1 = testClass.FindTestMethod("TestMethod1"); testMethod2 = testClass.FindTestMethod("TestMethod2"); }
public override void SetUp() { base.SetUp(); resultChangedCalled = false; AddCodeFile("test.cs", @" using NUnit.Framework; namespace RootNamespace.Tests { [TestFixture] class MyTestFixture { [Test] public void TestMethod() { } } }"); testClass = (NUnitTestClass)testProject.NestedTests.Single().NestedTests.Single(); testMethod = (NUnitTestMethod)testClass.NestedTests.Single(); }
public override void SetUp() { base.SetUp(); AddCodeFileInNamespace("base.cs", @" abstract class MyTestFixtureBase { [Test] public void MyTest() {} [Test] public void MyTest() {} }"); AddCodeFileInNamespace("derived.cs", @" class MyTestFixture : MyTestFixtureBase { [Test] public void MyTest() {} [Test] public void MyTest() {} }"); testClass = testProject.NestedTests.Cast<NUnitTestClass>().Single(c => c.ClassName == "MyTestFixture"); baseMethod = testClass.FindTestMethod("MyTestFixtureBase.MyTest"); derivedMethod = testClass.FindTestMethod("MyTest"); }
public void UpdateTestClass(ITypeDefinition typeDefinition) { fullTypeName = typeDefinition.FullTypeName; if (this.NestedTestsInitialized) { int baseClassIndex = 0; foreach (IType baseType in typeDefinition.GetNonInterfaceBaseTypes()) { ITypeDefinition baseTypeDef = baseType.GetDefinition(); // Check that the base type isn't equal to System.Object or the current class itself if (baseTypeDef == null || baseTypeDef == typeDefinition || baseTypeDef.KnownTypeCode == KnownTypeCode.Object) continue; if (baseTypeDef.DeclaringTypeDefinition != null) continue; // we only support inheriting from top-level classes var baseClassName = baseTypeDef.FullTypeName; if (baseClassIndex < baseClassNames.Count && baseClassName == baseClassNames[baseClassIndex]) { // base class is already in the list, just keep it baseClassIndex++; } else { // base class is not in the list, or the remaining portion of the list differs // remove remaining portion of the list: RemoveBaseClasses(baseClassIndex); // Add new base class: parentProject.RegisterInheritedClass(baseClassName, this); baseClassNames.Add(baseClassName); baseClassIndex++; } } HashSet<ITest> newOrUpdatedNestedTests = new HashSet<ITest>(); // Update nested test classes: foreach (ITypeDefinition nestedClass in typeDefinition.NestedTypes) { if (!NUnitTestFramework.IsTestClass(nestedClass)) continue; NUnitTestClass nestedTestClass = FindNestedTestClass(nestedClass.Name, nestedClass.TypeParameterCount); if (nestedTestClass != null) { nestedTestClass.UpdateTestClass(nestedClass); } else { nestedTestClass = new NUnitTestClass(parentProject, nestedClass.FullTypeName); this.NestedTestCollection.Add(nestedTestClass); } newOrUpdatedNestedTests.Add(nestedTestClass); } // Get methods (not operators etc.) foreach (IMethod method in typeDefinition.GetMethods(m => m.SymbolKind == SymbolKind.Method)) { if (!NUnitTestFramework.IsTestMethod(method)) continue; IUnresolvedMethod unresolvedMethod = (IUnresolvedMethod)method.UnresolvedMember; string methodNameWithDeclaringType; FullTypeName derivedFixture; if (method.DeclaringTypeDefinition == typeDefinition) { derivedFixture = default(FullTypeName); // method is not inherited methodNameWithDeclaringType = method.Name; } else { if (method.DeclaringTypeDefinition == null) continue; derivedFixture = fullTypeName; // method is inherited methodNameWithDeclaringType = method.DeclaringTypeDefinition.Name + "." + method.Name; } NUnitTestMethod testMethod; if (method.IsOverride) { testMethod = FindTestMethodWithShortName(method.Name); } else { testMethod = FindTestMethod(methodNameWithDeclaringType); } if (testMethod != null) { testMethod.UpdateTestMethod(unresolvedMethod, derivedFixture); } else { testMethod = new NUnitTestMethod(parentProject, unresolvedMethod, derivedFixture); this.NestedTestCollection.Add(testMethod); } newOrUpdatedNestedTests.Add(testMethod); } // Remove all tests that weren't contained in the new type definition anymore: this.NestedTestCollection.RemoveAll(t => !newOrUpdatedNestedTests.Contains(t)); } }
public void TestMethodSpecifiedInInitialize() { var method = new DefaultUnresolvedMethod(new DefaultUnresolvedTypeDefinition("TestFixture"), "Test"); var testMethod = new NUnitTestMethod(testProject, method); NUnitConsoleApplication app = new NUnitConsoleApplication(new[] { testMethod }); app.NoLogo = false; app.ShadowCopy = true; app.NoXmlOutputFile = false; string expectedCommandLine = "\"C:\\Projects\\MyTests\\MyTests.dll\" /run=\"TestFixture.Test\""; Assert.AreEqual(expectedCommandLine, app.GetArguments()); }
public void UpdateTestClass(ITypeDefinition typeDefinition) { fullTypeName = typeDefinition.FullTypeName; if (this.NestedTestsInitialized) { int baseClassIndex = 0; foreach (IType baseType in typeDefinition.GetNonInterfaceBaseTypes()) { ITypeDefinition baseTypeDef = baseType.GetDefinition(); // Check that the base type isn't equal to System.Object or the current class itself if (baseTypeDef == null || baseTypeDef == typeDefinition || baseTypeDef.KnownTypeCode == KnownTypeCode.Object) { continue; } if (baseTypeDef.DeclaringTypeDefinition != null) { continue; // we only support inheriting from top-level classes } var baseClassName = baseTypeDef.FullTypeName; if (baseClassIndex < baseClassNames.Count && baseClassName == baseClassNames[baseClassIndex]) { // base class is already in the list, just keep it baseClassIndex++; } else { // base class is not in the list, or the remaining portion of the list differs // remove remaining portion of the list: RemoveBaseClasses(baseClassIndex); // Add new base class: parentProject.RegisterInheritedClass(baseClassName, this); baseClassNames.Add(baseClassName); baseClassIndex++; } } HashSet <ITest> newOrUpdatedNestedTests = new HashSet <ITest>(); // Update nested test classes: foreach (ITypeDefinition nestedClass in typeDefinition.NestedTypes) { if (!NUnitTestFramework.IsTestClass(nestedClass)) { continue; } NUnitTestClass nestedTestClass = FindNestedTestClass(nestedClass.Name, nestedClass.TypeParameterCount); if (nestedTestClass != null) { nestedTestClass.UpdateTestClass(nestedClass); } else { nestedTestClass = new NUnitTestClass(parentProject, nestedClass.FullTypeName); this.NestedTestCollection.Add(nestedTestClass); } newOrUpdatedNestedTests.Add(nestedTestClass); } // Get methods (not operators etc.) foreach (IMethod method in typeDefinition.GetMethods(m => m.SymbolKind == SymbolKind.Method)) { if (!NUnitTestFramework.IsTestMethod(method)) { continue; } IUnresolvedMethod unresolvedMethod = (IUnresolvedMethod)method.UnresolvedMember; string methodNameWithDeclaringType; FullTypeName derivedFixture; if (method.DeclaringTypeDefinition == typeDefinition) { derivedFixture = default(FullTypeName); // method is not inherited methodNameWithDeclaringType = method.Name; } else { if (method.DeclaringTypeDefinition == null) { continue; } derivedFixture = fullTypeName; // method is inherited methodNameWithDeclaringType = method.DeclaringTypeDefinition.Name + "." + method.Name; } NUnitTestMethod testMethod; if (method.IsOverride) { testMethod = FindTestMethodWithShortName(method.Name); } else { testMethod = FindTestMethod(methodNameWithDeclaringType); } if (testMethod != null) { testMethod.UpdateTestMethod(unresolvedMethod, derivedFixture); } else { testMethod = new NUnitTestMethod(parentProject, unresolvedMethod, derivedFixture); this.NestedTestCollection.Add(testMethod); } newOrUpdatedNestedTests.Add(testMethod); } // Remove all tests that weren't contained in the new type definition anymore: this.NestedTestCollection.RemoveAll(t => !newOrUpdatedNestedTests.Contains(t)); } }