예제 #1
0
        public void XUnit2TestGeneratorProvider_ShouldInitializeTestOutputHelperFieldInConstructor()
        {
            // ARRANGE
            var document = ParseDocumentFromString(SampleFeatureFile);
            var provider = new XUnit2TestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp), new ProjectSettings()
            {
                DefaultNamespace = "Target"
            });
            var converter = provider.CreateUnitTestConverter();

            // ACT
            var code = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

            code.Should().NotBeNull();

            // ASSERT
            var classConstructor = code.Class().Members().Single(m => m.Name == ".ctor");

            classConstructor.Should().NotBeNull();
            classConstructor.Parameters.Count.Should().Be(3);
            classConstructor.Parameters[2].Type.BaseType.Should().Be("Xunit.Abstractions.ITestOutputHelper");
            classConstructor.Parameters[2].Name.Should().Be("testOutputHelper");

            var initOutputHelper = classConstructor.Statements.OfType <CodeAssignStatement>().First();

            initOutputHelper.Should().NotBeNull();
            ((CodeFieldReferenceExpression)initOutputHelper.Left).FieldName.Should().Be("_testOutputHelper");
            ((CodeVariableReferenceExpression)initOutputHelper.Right).VariableName.Should().Be("testOutputHelper");
        }
예제 #2
0
        public void XUnit2TestGeneratorProvider_Should_register_testOutputHelper_on_scenario_setup()
        {
            // ARRANGE
            var document = ParseDocumentFromString(SampleFeatureFile);
            var provider = new XUnit2TestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp), new ProjectSettings()
            {
                DefaultNamespace = "Target"
            });
            var converter = provider.CreateUnitTestConverter();

            // ACT
            var code = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

            // ASSERT
            code.Should().NotBeNull();
            var scenarioStartMethod = code.Class().Members().Single(m => m.Name == @"ScenarioInitialize");

            scenarioStartMethod.Statements.Count.Should().Be(2);

            var expression = scenarioStartMethod.Statements[1].Should().BeOfType <CodeExpressionStatement>()
                             .Which.Expression.Should().BeOfType <CodeMethodInvokeExpression>().Which;

            expression.Parameters[0].Should().BeOfType <CodeVariableReferenceExpression>()
            .Which.VariableName.Should().Be("_testOutputHelper");

            var method = expression.Method;

            method.TargetObject.Should().BeOfType <CodePropertyReferenceExpression>()
            .Which.PropertyName.Should().Be("ScenarioContainer");
            method.MethodName.Should().Be("RegisterInstanceAs");
            method.TypeArguments.Should().NotBeNullOrEmpty();
            method.TypeArguments[0].BaseType.Should().Be("Xunit.Abstractions.ITestOutputHelper");
        }
예제 #3
0
        public void XUnit2TestGeneratorProvider_WithFeatureWithMatchingTag_ShouldAddNonParallelizableCollectionAttribute()
        {
            // ARRANGE
            var document = ParseDocumentFromString(@"
            @nonparallelizable
            Feature: Sample feature file

            Scenario: Simple scenario
                Given there is something");

            var provider = new XUnit2TestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp), new ProjectSettings()
            {
                DefaultNamespace = "Target"
            });
            var featureGenerator = provider.CreateFeatureGenerator(addNonParallelizableMarkerForTags: new string[] { "nonparallelizable" });

            // ACT
            var code = featureGenerator.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

            // ASSERT
            var attributes = code.Class().CustomAttributes().ToArray();

            attributes.Should().ContainSingle(a => a.Name == XUnitCollectionAttribute);
            var collectionAttribute = attributes.Single(a => a.Name == XUnitCollectionAttribute);

            collectionAttribute.Arguments.Should().HaveCount(1);
            collectionAttribute.Arguments[0].Value.Should().BeEquivalentTo(new CodePrimitiveExpression("SpecFlowNonParallelizableFeatures"));
        }
        public void Should_initialize_testOutputHelper_field_in_constructor()
        {
            SpecFlowGherkinParser parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));

            using (var reader = new StringReader(SampleFeatureFile))
            {
                SpecFlowDocument document = parser.Parse(reader, null);
                Assert.IsNotNull(document);

                var provider = new XUnit2TestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));

                var           converter = provider.CreateUnitTestConverter();
                CodeNamespace code      = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

                Assert.IsNotNull(code);
                var classContructor = code.Class().Members().Single(m => m.Name == ".ctor");
                classContructor.Should().NotBeNull();
                classContructor.Parameters.Count.Should().Be(2);
                classContructor.Parameters[1].Type.BaseType.Should().Be("Xunit.Abstractions.ITestOutputHelper");
                classContructor.Parameters[1].Name.Should().Be("testOutputHelper");

                var initOutputHelper = classContructor.Statements.OfType <CodeAssignStatement>().First();
                initOutputHelper.Should().NotBeNull();
                ((CodeFieldReferenceExpression)(initOutputHelper.Left)).FieldName.Should().Be("_testOutputHelper");
                ((CodeVariableReferenceExpression)(initOutputHelper.Right)).VariableName.Should().Be("testOutputHelper");
            }
        }
        public void Should_register_testOutputHelper_on_scenario_setup()
        {
            SpecFlowGherkinParser parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));

            using (var reader = new StringReader(SampleFeatureFile))
            {
                SpecFlowDocument document = parser.Parse(reader, null);
                Assert.IsNotNull(document);

                var provider = new XUnit2TestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));

                var           converter = provider.CreateUnitTestConverter();
                CodeNamespace code      = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

                Assert.IsNotNull(code);
                var scenarioStartMethod = code.Class().Members().Single(m => m.Name == @"ScenarioSetup");

                scenarioStartMethod.Statements.Count.Should().Be(2);
                var expression = (scenarioStartMethod.Statements[1] as CodeExpressionStatement).Expression;
                var method     = (expression as CodeMethodInvokeExpression).Method;
                (method.TargetObject as CodePropertyReferenceExpression).PropertyName.Should().Be("ScenarioContainer");
                method.MethodName.Should().Be("RegisterInstanceAs");
                method.TypeArguments.Should().NotBeNullOrEmpty();
                method.TypeArguments[0].BaseType.Should().Be("Xunit.Abstractions.ITestOutputHelper");

                ((expression as CodeMethodInvokeExpression).Parameters[0] as CodeVariableReferenceExpression).VariableName.Should().Be("_testOutputHelper");
            }
        }
        public void XUnit2TestGeneratorProvider_ShouldSetSkipAttributeForTheory()
        {
            // Arrange
            var provider = new XUnit2TestGeneratorProvider(new CodeDomHelper(new CSharpCodeProvider()));

            // Act
            var codeMemberMethod = new CodeMemberMethod
            {
                CustomAttributes =
                    new CodeAttributeDeclarationCollection(
                        new[] { new CodeAttributeDeclaration(XUnit2TestGeneratorProvider.THEORY_ATTRIBUTE) })
            };

            provider.SetTestMethodIgnore(null, codeMemberMethod);

            // Assert
            var modifiedAttribute = codeMemberMethod.CustomAttributes.OfType <CodeAttributeDeclaration>()
                                    .FirstOrDefault(a => a.Name == XUnit2TestGeneratorProvider.THEORY_ATTRIBUTE);

            modifiedAttribute.Should().NotBeNull();


            var attribute = modifiedAttribute.Arguments
                            .OfType <CodeAttributeArgument>()
                            .FirstOrDefault(a => a.Name == XUnit2TestGeneratorProvider.THEORY_ATTRIBUTE_SKIP_PROPERTY_NAME);

            attribute.Should().NotBeNull();


            var primitiveExpression = attribute.Value as CodePrimitiveExpression;

            primitiveExpression.Should().NotBeNull();
            primitiveExpression.Value.Should().Be(XUnit2TestGeneratorProvider.SKIP_REASON);
        }
예제 #7
0
        public void XUnit2TestGeneratorProvider_ShouldSetDisplayNameForTheoryAttribute()
        {
            // Arrange
            var provider = new XUnit2TestGeneratorProvider(new CodeDomHelper(new CSharpCodeProvider()), new ProjectSettings()
            {
                DefaultNamespace = "Target"
            });
            var context = new Generator.TestClassGenerationContext(
                unitTestGeneratorProvider: null,
                document: new SpecFlowDocument(
                    feature: new SpecFlowFeature(
                        tags: null,
                        location: null,
                        language: null,
                        keyword: null,
                        name: "",
                        description: null,
                        children: null
                        ),
                    comments: null,
                    sourceFilePath: null),
                ns: null,
                testClass: null,
                testRunnerField: null,
                testClassInitializeMethod: null,
                testClassCleanupMethod: null,
                testInitializeMethod: null,
                testCleanupMethod: null,
                scenarioInitializeMethod: null,
                scenarioStartMethod: null,
                scenarioCleanupMethod: null,
                featureBackgroundMethod: null,
                generateRowTests: false);
            var codeMemberMethod = new CodeMemberMethod();

            // Act
            provider.SetRowTest(context, codeMemberMethod, "Foo");

            // Assert
            var modifiedAttribute = codeMemberMethod.CustomAttributes.OfType <CodeAttributeDeclaration>()
                                    .FirstOrDefault(a => a.Name == "Xunit.SkippableTheoryAttribute");

            modifiedAttribute.Should().NotBeNull();


            var attribute = modifiedAttribute.Arguments
                            .OfType <CodeAttributeArgument>()
                            .FirstOrDefault(a => a.Name == "DisplayName");

            attribute.Should().NotBeNull();


            var primitiveExpression = attribute.Value as CodePrimitiveExpression;

            primitiveExpression.Should().NotBeNull();
            primitiveExpression.Value.Should().Be("Foo");
        }
예제 #8
0
        public void XUnit2TestGeneratorProvider_ExampleIdentifier_ShouldSetInlineDataAttributesCorrectly()
        {
            // ARRANGE
            const string sampleFeatureFileWithMultipleExampleSets = @"
              Feature: Sample feature file
              
              Scenario: Simple scenario
                  Given there is something
                  When I do something
                  Then something should happen
              
              @mytag
              Scenario Outline: Simple Scenario Outline
                  Given there is something
                      """"""
                        long string
                      """"""
                  When I do <what>
                      | foo | bar |
                      | 1   | 2   |
                  Then something should happen
              Examples:
                  | what           |
                  | something      |
                  | something else |
              Examples:
                  | what           |
                  | another        |
                  | and another    |
";

            var document = ParseDocumentFromString(sampleFeatureFileWithMultipleExampleSets);
            var sampleTestGeneratorProvider = new XUnit2TestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp), new ProjectSettings()
            {
                DefaultNamespace = "Target"
            });
            var converter = sampleTestGeneratorProvider.CreateUnitTestConverter();

            // ACT
            var code = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

            // ASSERT
            var inlineDataAttributes = code.Class()
                                       .Members()
                                       .Single(m => m.Name == "SimpleScenarioOutline")
                                       .CustomAttributes()
                                       .Where(a => a.Name == XUnitInlineDataAttribute)
                                       .ToArray();

            inlineDataAttributes.Should().ContainSingle(attribute => attribute.ArgumentValues().First() as string == "something");
            inlineDataAttributes.Should().ContainSingle(attribute => attribute.ArgumentValues().First() as string == "something else");
            inlineDataAttributes.Should().ContainSingle(attribute => attribute.ArgumentValues().First() as string == "another");
            inlineDataAttributes.Should().ContainSingle(attribute => attribute.ArgumentValues().First() as string == "and another");
        }
예제 #9
0
        public void XUnit2TestGeneratorProvider_ShouldHaveParallelExecutionTrait()
        {
            var provider = new XUnit2TestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp), new ProjectSettings()
            {
                DefaultNamespace = "Target"
            });

            provider.GetTraits()
            .HasFlag(UnitTestGeneratorTraits.ParallelExecution)
            .Should()
            .BeTrue("trait ParallelExecution was not found");
        }
        public void Should_set_displayname_theory_attribute()
        {
            // Arrange
            var provider = new XUnit2TestGeneratorProvider(new CodeDomHelper(new CSharpCodeProvider()));
            var context  = new Generator.TestClassGenerationContext(
                unitTestGeneratorProvider: null,
                document: new Parser.SpecFlowDocument(
                    feature: new SpecFlowFeature(
                        tags: null,
                        location: null,
                        language: null,
                        keyword: null,
                        name: "",
                        description: null,
                        children: null
                        ),
                    comments: null,
                    sourceFilePath: null),
                ns: null,
                testClass: null,
                testRunnerField: null,
                testClassInitializeMethod: null,
                testClassCleanupMethod: null,
                testInitializeMethod: null,
                testCleanupMethod: null,
                scenarioInitializeMethod: null,
                scenarioStartMethod: null,
                scenarioCleanupMethod: null,
                featureBackgroundMethod: null,
                generateRowTests: false);
            var codeMemberMethod = new CodeMemberMethod();

            // Act
            provider.SetRowTest(context, codeMemberMethod, "Foo");

            // Assert
            var modifiedAttribute = codeMemberMethod.CustomAttributes.OfType <CodeAttributeDeclaration>()
                                    .FirstOrDefault(a => a.Name == "Xunit.TheoryAttribute");

            Assert.That(modifiedAttribute, Is.Not.Null);
            var attribute = modifiedAttribute.Arguments.OfType <CodeAttributeArgument>()
                            .FirstOrDefault(a => a.Name == "DisplayName");

            Assert.That(attribute, Is.Not.Null);

            var primitiveExpression = attribute.Value as CodePrimitiveExpression;

            Assert.That(primitiveExpression, Is.Not.Null);
            Assert.That(primitiveExpression.Value, Is.EqualTo("Foo"));
        }
        public void XUnit2TestGeneratorProvider_Should_add_testOutputHelper_field_in_class()
        {
            // ARRANGE
            var document  = ParseDocumentFromString(SampleFeatureFile);
            var provider  = new XUnit2TestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));
            var converter = provider.CreateUnitTestConverter();

            // ACT
            var code = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

            // ASSERT
            code.Should().NotBeNull();
            var loggerInstance = code.Class().Members.OfType <CodeMemberField>().First(m => m.Name == @"_testOutputHelper");

            loggerInstance.Type.BaseType.Should().Be("Xunit.Abstractions.ITestOutputHelper");
            loggerInstance.Attributes.Should().Be(MemberAttributes.Private | MemberAttributes.Final);
        }
        public void Should_add_testOutputHelper_field_in_class()
        {
            SpecFlowGherkinParser parser = new SpecFlowGherkinParser(new CultureInfo("en-US"));

            using (var reader = new StringReader(SampleFeatureFile))
            {
                SpecFlowDocument document = parser.Parse(reader, null);
                Assert.IsNotNull(document);

                var provider = new XUnit2TestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp));

                var           converter = provider.CreateUnitTestConverter();
                CodeNamespace code      = converter.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

                Assert.IsNotNull(code);
                var loggerInstance = code.Class().Members.OfType <CodeMemberField>().First(m => m.Name == @"_testOutputHelper");
                loggerInstance.Type.BaseType.Should().Be("Xunit.Abstractions.ITestOutputHelper");
                loggerInstance.Attributes.Should().Be(MemberAttributes.Private | MemberAttributes.Final);
            }
        }
예제 #13
0
        public void XUnit2TestGeneratorProvider_WithFeatureWithNoMatchingTag_ShouldNotAddNonParallelizableCollectionAttribute()
        {
            // ARRANGE
            var document = ParseDocumentFromString(@"
            Feature: Sample feature file

            Scenario: Simple scenario
                Given there is something");

            var provider = new XUnit2TestGeneratorProvider(new CodeDomHelper(CodeDomProviderLanguage.CSharp), new ProjectSettings()
            {
                DefaultNamespace = "Target"
            });
            var featureGenerator = provider.CreateFeatureGenerator(addNonParallelizableMarkerForTags: new string[] { "nonparallelizable" });

            // ACT
            var code = featureGenerator.GenerateUnitTestFixture(document, "TestClassName", "Target.Namespace");

            // ASSERT
            var attributes = code.Class().CustomAttributes().ToArray();

            attributes.Should().NotContain(a => a.Name == XUnitCollectionAttribute);
        }