Beispiel #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");
        }
Beispiel #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");
        }
        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");
            }
        }
Beispiel #5
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");
        }
        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);
            }
        }