public void RunAllTestsInDocument_ShouldExtractAllProjectReferences_And_PassItTo_TestSandbox() { // arrange var project = CreateProject("SampleTestsProject"); string[] allProjectReferences = { "A" }; _solutionExplorerMock.GetAllProjectReferences(project.Name).Returns(allProjectReferences); var testNode = CSharpSyntaxTree.ParseText("[TestFixture]class HelloWorldTests{" + " [Test] public void TestMethod()" + "{}" + "}"); var testClass = testNode.GetRoot().GetClassDeclarationSyntax(); var fixtureDetails = new TestFixtureDetails(); var testCase = new TestCase(fixtureDetails) { SyntaxNode = testClass.GetPublicMethods().Single() }; fixtureDetails.Cases.Add(testCase); _testExtractorMock.GetTestClasses(Arg.Any<CSharpSyntaxNode>()) .Returns(new[] { testClass }); _testExtractorMock.GetTestFixtureDetails(testClass, Arg.Any<ISemanticModel>()).Returns(fixtureDetails); var rewrittenDocument = new RewrittenDocument(testNode, null, false); // act _sut.RunAllTestsInDocument(rewrittenDocument, null, project, new string[0]); // assert _testExecutorEngineMock.Received(1). RunTestFixture(Arg.Is<string[]>(x => x[0] == allProjectReferences[0]), Arg.Any<TestFixtureExecutionScriptParameters>()); }
public void RunAllTestsInDocument_ShouldExtractAllTestCases() { // arrange var project = CreateProject("SampleTestsProject"); var semanticModel = Substitute.For<ISemanticModel>(); var testNode = CSharpSyntaxTree.ParseText("[TestFixture]class HelloWorldTests{" + " [Test] public void TestMethod()" + "{}" + "}"); var testClass = testNode.GetRoot().GetClassDeclarationSyntax(); var fixtureDetails = new TestFixtureDetails(); var testCase = new TestCase(fixtureDetails) { SyntaxNode = testClass.GetPublicMethods().Single() }; fixtureDetails.Cases.Add(testCase); _testExtractorMock.GetTestClasses(Arg.Any<CSharpSyntaxNode>()) .Returns(new[] { testClass }); _testExtractorMock.GetTestFixtureDetails(Arg.Any<ClassDeclarationSyntax>(), Arg.Any<ISemanticModel>()).Returns(fixtureDetails); var rewrittenDocument = new RewrittenDocument(testNode, null, false); // act _sut.RunAllTestsInDocument(rewrittenDocument, semanticModel, project, new string[0]); // assert _testExtractorMock.Received(1).GetTestFixtureDetails(testClass, semanticModel); }
private static List<TestCase> ExtractMethodsFromAttributes(TestFixtureDetails testFixture, ISemanticModel semanticModel, AttributeSyntax attribute) { List<TestCase> methodTestCases=new List<TestCase>(); if (attribute.Name.ToString() == "Test") { var testCase = ExtractTest(attribute, testFixture); if (testCase != null) methodTestCases.Add(testCase); } else if (attribute.Name.ToString() == "TestCase") { var testCase = ExtractTestCase(attribute, testFixture, semanticModel); if (testCase != null) methodTestCases.Add(testCase); } else if (attribute.Name.ToString() == "SetUp") testFixture.TestSetUpMethodName = GetAttributeMethod(attribute).Identifier.ValueText; else if (attribute.Name.ToString() == "TestFixtureSetUp") testFixture.TestFixtureSetUpMethodName = GetAttributeMethod(attribute).Identifier.ValueText; else if (attribute.Name.ToString() == "TearDown") testFixture.TestTearDownMethodName = GetAttributeMethod(attribute).Identifier.ValueText; else if (attribute.Name.ToString() == "TestFixtureTearDown") testFixture.TestFixtureTearDownMethodName = GetAttributeMethod(attribute).Identifier.ValueText; return methodTestCases; }
private static TestCase ExtractTest(AttributeSyntax attribute, TestFixtureDetails testFixture) { var methodDeclarationSyntax = GetAttributeMethod(attribute); bool isAsync = IsAsync(methodDeclarationSyntax); if (isAsync && IsVoid(methodDeclarationSyntax)) return null; var testCase = new TestCase(testFixture) { SyntaxNode = methodDeclarationSyntax, IsAsync = isAsync, MethodName = methodDeclarationSyntax.Identifier.ValueText, Arguments = new string[0] }; return testCase; }
public void RunAllTestsInDocument_ShouldReturn_One_Line_Coverage_When_ThereIsOneClass_And_OneTestCase_And_ItContains_One_LineCoverage() { // arrange var testNode = CSharpSyntaxTree.ParseText("[TestFixture]class HelloWorldTests{" + " [Test] public void TestMethod()" + "{}" + "}"); var testClass = testNode.GetRoot().GetClassDeclarationSyntax(); var fixtureDetails = new TestFixtureDetails(); var testCase = new TestCase(fixtureDetails) { SyntaxNode = testClass.GetPublicMethods().Single(), MethodName = "TestMethod" }; fixtureDetails.Cases.Add(testCase); _testExtractorMock.GetTestClasses(Arg.Any<CSharpSyntaxNode>()) .Returns(new[] { testClass }); _testExtractorMock.GetTestFixtureDetails(testClass, Arg.Any<ISemanticModel>()).Returns(fixtureDetails); var testRunResultMock = Substitute.For<ITestRunResult>(); testRunResultMock.TestName.Returns("TestMethod"); var expectedLineCoverage = new[] { new LineCoverage() }; testRunResultMock.GetCoverage(Arg.Any<SyntaxNode>(), Arg.Any<string>(), Arg.Any<string>()) .Returns(expectedLineCoverage); _testExecutorEngineMock.RunTestFixture(Arg.Any<string[]>(), Arg.Any<TestFixtureExecutionScriptParameters>()). Returns(new[] { testRunResultMock }); var project = CreateProject("SampleTestsProject"); var rewrittenDocument = new RewrittenDocument(testNode, null, false); // act LineCoverage[] output = _sut.RunAllTestsInDocument(rewrittenDocument, null, project, new string[0]); // assert Assert.That(output, Is.SameAs(output)); }
public TestCase(TestFixtureDetails testFixture) { TestFixture = testFixture; Arguments=new object[0]; }
private LineCoverage[] RunTestFixture(TestFixtureDetails testFixtureDetails, CompiledTestFixtureInfo compiledTestFixtureInfo, string testProjectName) { var testFixtureExecutionScriptParameters = new TestFixtureExecutionScriptParameters { TestFixtureTypeFullName = testFixtureDetails.FullClassName, TestFixtureAssemblyName=testFixtureDetails.AssemblyName, TestCases = new List<TestExecutionScriptParameters>(), TestFixtureSetUpMethodName = testFixtureDetails.TestFixtureSetUpMethodName, TestFixtureTearDownMethodName = testFixtureDetails.TestFixtureTearDownMethodName, TestSetUpMethodName = testFixtureDetails.TestSetUpMethodName, TestTearDownMethodName = testFixtureDetails.TestTearDownMethodName, }; foreach (var testCase in testFixtureDetails.Cases) { var scriptParameters = new TestExecutionScriptParameters { TestName = testCase.MethodName, TestParameters = testCase.Arguments, IsAsync = testCase.IsAsync }; testFixtureExecutionScriptParameters.TestCases.Add(scriptParameters); } var results = _testExecutorScriptEngine.RunTestFixture(compiledTestFixtureInfo.AllReferences, testFixtureExecutionScriptParameters); return GetCoverage(testFixtureDetails, compiledTestFixtureInfo, testProjectName, results); }
private static LineCoverage[] GetCoverage(TestFixtureDetails testFixtureDetails, CompiledTestFixtureInfo compiledTestFixtureInfo, string testProjectName, ITestRunResult[] results) { var coverage = new List<LineCoverage>(results.Length); foreach (var testRunResult in results) { var methodSyntaxNode = testFixtureDetails.Cases. FirstOrDefault(x => x.MethodName == testRunResult.TestName)?.SyntaxNode; var partialCoverage = testRunResult.GetCoverage(methodSyntaxNode, testProjectName, compiledTestFixtureInfo.TestDocumentPath); coverage.AddRange(partialCoverage); } return coverage.OrderBy(x => x.TestPath).ToArray(); }
private static TestCase ExtractTestCase(AttributeSyntax attribute, TestFixtureDetails testFixture, ISemanticModel semanticModel) { var methodDeclarationSyntax = GetAttributeMethod(attribute); bool isAsync = IsAsync(methodDeclarationSyntax); if (isAsync && IsVoid(methodDeclarationSyntax)) return null; var testCase = new TestCase(testFixture); testCase.IsAsync = isAsync; testCase.Arguments = new object[attribute.ArgumentList.Arguments.Count]; for (int i = 0; i < testCase.Arguments.Length; i++) { AttributeArgumentSyntax testCaseArg = attribute.ArgumentList.Arguments[i]; object constantValue = semanticModel.GetConstantValue(testCaseArg.Expression); if (constantValue != null) testCase.Arguments[i] = constantValue; else testCase.Arguments[i] = testCaseArg.Expression.GetFirstToken().Value; } testCase.SyntaxNode = methodDeclarationSyntax; testCase.MethodName = methodDeclarationSyntax.Identifier.ValueText; return testCase; }
private TestFixtureDetails ExtractTests(ClassDeclarationSyntax testClass, ISemanticModel semanticModel) { var testFixture = new TestFixtureDetails(); testFixture.FullClassName = semanticModel.GetFullName(testClass); foreach (MethodDeclarationSyntax methodNode in testClass.DescendantNodes().OfType<MethodDeclarationSyntax>()) { ExtractMethodTests(testFixture, methodNode, semanticModel); } return testFixture; }
private void ExtractMethodTests(TestFixtureDetails testFixture, MethodDeclarationSyntax methodNode, ISemanticModel semanticModel) { var allMethodTestCases = new List<TestCase>(); foreach (AttributeSyntax attribute in methodNode.DescendantNodes().OfType<AttributeSyntax>()) { var methodTestCases=ExtractMethodsFromAttributes(testFixture, semanticModel, attribute); allMethodTestCases.AddRange(methodTestCases); } if (allMethodTestCases.Count > 0) { int maxPars = allMethodTestCases.Max(x => x.Arguments.Length); testFixture.Cases.AddRange(allMethodTestCases.Where(x => x.Arguments.Length == maxPars)); } }