public override void SetUp() { base.SetUp(); AddCodeFile("test.cs", @" using NUnit.Framework; namespace MyTests { class A { class InnerTest { [Test] public void M() {} } } } "); originalA = testProject.GetTestClass(new FullTypeName("MyTests.A")); UpdateCodeFile("test.cs", @" using NUnit.Framework; namespace MyTests { class A { class InnerTestMod { [Test] public void M() {} } } } "); }
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 MyTests { class A { class InnerATest { [Test] public void FooBar() {} } } } "); // The members should be changed on the existing TestClass instance, // so grab the reference in before updating. innerTestClass = testProject.GetTestClass(new FullTypeName("MyTests.A+InnerATest")); UpdateCodeFile("test.cs", @" using NUnit.Framework; namespace MyTests { class A { class InnerATest { [Test] public void FooBarRenamed() {} [TestFixture] class InnerInnerTest {} } } } "); }
public override void SetUp() { base.SetUp(); AddCodeFile("file1.cs", program); myTestFixture = (NUnitTestClass)testProject.NestedTests.Single(); myTestFixture.EnsureNestedTestsInitialized(); AddCodeFile("file2.cs", program); }
ITest GetTestForEntityInClass(NUnitTestClass c, IEntity entity) { if (c == null) return null; if (entity.SymbolKind == SymbolKind.TypeDefinition) return c; else if (entity.SymbolKind == SymbolKind.Method) return c.FindTestMethod(entity.Name); else return null; }
public override void SetUp() { base.SetUp(); AddCodeFileInNamespace("base1.cs", "class Base1 { [Test] public void Base1Test() {} }"); AddCodeFileInNamespace("base2.cs", "class Base2 { [Test] public void Base2Test() {} }"); AddCodeFileInNamespace("middle.cs", "class Middle : Base1 { }"); AddCodeFileInNamespace("derived.cs", "[TestFixture] class Derived : Middle { }"); testProject.EnsureNestedTestsInitialized(); derived = testProject.NestedTests.OfType<NUnitTestClass>().Single(c => c.ClassName == "Derived"); derived.EnsureNestedTestsInitialized(); }
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(); AddCodeFileInNamespace("base.cs", @" [TestFixture] class TestFixtureBase { [Test] public void BaseMethod() {} }"); AddCodeFileInNamespace("derived.cs", @" [TestFixture] class MyTestFixture : TestFixtureBase { [Test] public void DerivedMethod() {} }"); testClass = testProject.GetTestClass(new FullTypeName("RootNamespace.MyTestFixture")); testClass.EnsureNestedTestsInitialized(); }
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", @" class BaseClass { [Test] public virtual void VirtualTestMethod() {} [Test] public virtual void VirtualNonOverriddenTestMethod() {} }"); AddCodeFileInNamespace("derived.cs", @" class DerivedClass : BaseClass { [Test] public override void VirtualTestMethod() {} }"); baseClass = testProject.NestedTests.Cast<NUnitTestClass>().Single(c => c.ClassName == "BaseClass"); derivedClass = testProject.NestedTests.Cast<NUnitTestClass>().Single(c => c.ClassName == "DerivedClass"); derivedClassMethodNames = derivedClass.NestedTests.Cast<NUnitTestMethod>().Select(m => m.MethodNameWithDeclaringTypeForInheritedTests).ToList(); }
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 override void SetUp() { base.SetUp(); AddCodeFileInNamespace("derived.cs", @" [TestFixture] public class CecilLayerTests : ReflectionOrCecilLayerTests { }"); AddCodeFileInNamespace("base.cs", @" public abstract class ReflectionOrCecilLayerTests { [Test] public void InheritanceTests() {} public void NonTestMethod() {} } "); testProject.EnsureNestedTestsInitialized(); cecilLayer = testProject.NestedTests.Cast<NUnitTestClass>().Single(c => c.ClassName == "CecilLayerTests"); testMembers = cecilLayer.NestedTests.Cast<NUnitTestMethod>().Select(m => m.MethodNameWithDeclaringTypeForInheritedTests).ToList(); }
ITest GetTestForEntityInClass(NUnitTestClass c, IEntity entity) { if (c == null) { return(null); } if (entity.SymbolKind == SymbolKind.TypeDefinition) { return(c); } else if (entity.SymbolKind == SymbolKind.Method) { return(c.FindTestMethod(entity.Name)); } else { return(null); } }
public override IEnumerable <ITest> GetTestsForEntity(IEntity entity) { ITypeDefinition typeDefinition = entity as ITypeDefinition ?? entity.DeclaringTypeDefinition; IEnumerable <ITest> tests; if (typeDefinition.IsAbstract) { tests = from c in entity.ParentAssembly.GetAllTypeDefinitions() where c.IsDerivedFrom(typeDefinition) select GetTestForEntityInClass(GetTestClass(c.FullTypeName), entity); } else { NUnitTestClass c = GetTestClass(typeDefinition.FullTypeName); tests = new [] { GetTestForEntityInClass(c, entity) }; } // GetTestForEntityInClass might return null, so filter those out: return(tests.Where(t => t != null)); }
public override void SetUp() { base.SetUp(); classesAdded = new List<NUnitTestClass>(); classesRemoved = new List<NUnitTestClass>(); // Create a project. AddCodeFile(mainFileName, @"using NUnit.Framework; namespace RootNamespace { [TestFixture] class MyTestFixture { } class NonNUnitTestClass { } }"); testClass = testProject.GetTestClass(new FullTypeName("RootNamespace.MyTestFixture")); testProject.NestedTests.CollectionChanged += testProject_TestClasses_CollectionChanged; }
public override void SetUp() { base.SetUp(); AddCodeFile("test.cs", @" using NUnit.Framework; namespace MyTests { public class A { public class InnerATest { [Test] public void FooBar() { } } } }"); outerClass = testProject.GetTestClass(new FullTypeName("MyTests.A")); innerClass = testProject.GetTestClass(new FullTypeName("MyTests.A+InnerATest")); }
public void RemoveInheritedClass(FullTypeName baseClassName, NUnitTestClass inheritedClass) { inheritedTestClasses.Remove(baseClassName.TopLevelTypeName, inheritedClass); }
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)); } }
protected List<string> GetTestMethodNames(NUnitTestClass testClass) { return testClass.NestedTests.OfType<NUnitTestMethod>().Select(m => m.MethodNameWithDeclaringTypeForInheritedTests).ToList(); }
public void TestInnerClassSpecifiedInInitialize() { NUnitTestClass innerTestClass = new NUnitTestClass(testProject, new FullTypeName("MyTests.TestFixture+InnerTest")); NUnitConsoleApplication app = new NUnitConsoleApplication(new [] { innerTestClass }); app.NoLogo = false; app.ShadowCopy = true; app.NoXmlOutputFile = false; string expectedCommandLine = "\"C:\\Projects\\MyTests\\MyTests.dll\" " + "/run=\"MyTests.TestFixture+InnerTest\""; Assert.AreEqual(expectedCommandLine, app.GetArguments()); }
public void RegisterInheritedClass(FullTypeName baseClassName, NUnitTestClass inheritedClass) { inheritedTestClasses.Add(baseClassName.TopLevelTypeName, inheritedClass); }
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 XmlOutputFileNameSpecifiedOnCommandLine() { UnitTestingOptions options = new UnitTestingOptions(new Properties()); options.CreateXmlOutputFile = true; NUnitTestClass testFixture = new NUnitTestClass(testProject, new FullTypeName("MyTests.TestFixture.MyTest")); NUnitConsoleApplication app = new NUnitConsoleApplication(new[] { testFixture }, options); app.NoLogo = false; app.ShadowCopy = true; string expectedCommandLine = "\"C:\\Projects\\MyTests\\MyTests.dll\" " + "/xml=\"C:\\Projects\\MyTests\\MyTests-TestResult.xml\" " + "/run=\"MyTests.TestFixture.MyTest\""; Assert.AreEqual(expectedCommandLine, app.GetArguments()); }