示例#1
0
        private void SetupTestClassInitializeMethod(TestClassGenerationContext generationContext)
        {
            var testClassInitializeMethod = generationContext.TestClassInitializeMethod;

            testClassInitializeMethod.ReturnType = new CodeTypeReference(typeof(Task));
            testClassInitializeMethod.Attributes = MemberAttributes.Public;
            testClassInitializeMethod.Name       = GeneratorConstants.TESTCLASS_INITIALIZE_NAME;

            _testGeneratorProvider.MarkCodeMemberMethodAsAsync(testClassInitializeMethod);

            _testGeneratorProvider.SetTestClassInitializeMethod(generationContext);

            //testRunner = await TestRunnerManager.GetTestRunnerAsync([class_name]);
            var testRunnerField = _scenarioPartHelper.GetTestRunnerExpression();

            var testRunnerParameters = new[]
            {
                new CodePrimitiveExpression(generationContext.TestClass.Name),
                new CodePrimitiveExpression(null),
                new CodePrimitiveExpression(null)
            };

            var getTestRunnerExpression = new CodeMethodInvokeExpression(
                new CodeTypeReferenceExpression(typeof(TestRunnerManager)),
                nameof(ITestRunnerManager.GetTestRunnerAsync), testRunnerParameters);

            _codeDomHelper.MarkCodeMethodInvokeExpressionAsAwait(getTestRunnerExpression);

            testClassInitializeMethod.Statements.Add(
                new CodeAssignStatement(
                    testRunnerField,
                    getTestRunnerExpression));

            //FeatureInfo featureInfo = new FeatureInfo("xxxx");
            testClassInitializeMethod.Statements.Add(
                new CodeVariableDeclarationStatement(typeof(FeatureInfo), "featureInfo",
                                                     new CodeObjectCreateExpression(typeof(FeatureInfo),
                                                                                    new CodeObjectCreateExpression(typeof(CultureInfo),
                                                                                                                   new CodePrimitiveExpression(generationContext.Feature.Language)),
                                                                                    new CodePrimitiveExpression(generationContext.Feature.Name),
                                                                                    new CodePrimitiveExpression(generationContext.Feature.Description),
                                                                                    new CodeFieldReferenceExpression(
                                                                                        new CodeTypeReferenceExpression("ProgrammingLanguage"),
                                                                                        _codeDomHelper.TargetLanguage.ToString()),
                                                                                    _scenarioPartHelper.GetStringArrayExpression(generationContext.Feature.Tags))));

            //await testRunner.OnFeatureStartAsync(featureInfo);
            var onFeatureStartExpression = new CodeMethodInvokeExpression(
                testRunnerField,
                nameof(ITestRunner.OnFeatureStartAsync),
                new CodeVariableReferenceExpression("featureInfo"));

            _codeDomHelper.MarkCodeMethodInvokeExpressionAsAwait(onFeatureStartExpression);

            testClassInitializeMethod.Statements.Add(onFeatureStartExpression);
        }
示例#2
0
        public void SetupFeatureBackground(TestClassGenerationContext generationContext)
        {
            if (!generationContext.Feature.HasFeatureBackground())
            {
                return;
            }

            var background = generationContext.Feature.Background;

            var backgroundMethod = generationContext.FeatureBackgroundMethod;

            backgroundMethod.ReturnType = new CodeTypeReference(typeof(Task));
            backgroundMethod.Attributes = MemberAttributes.Public;
            backgroundMethod.Name       = GeneratorConstants.BACKGROUND_NAME;

            _testGeneratorProvider.MarkCodeMemberMethodAsAsync(backgroundMethod);

            var statements = new List <CodeStatement>();

            using (new SourceLineScope(_specFlowConfiguration, _codeDomHelper, statements, generationContext.Document.SourceFilePath, background.Location))
            {
            }

            foreach (var step in background.Steps)
            {
                GenerateStep(generationContext, statements, step, null);
            }

            backgroundMethod.Statements.AddRange(statements.ToArray());
        }
        private CodeMemberMethod CreateScenatioOutlineTestMethod(TestClassGenerationContext generationContext, ScenarioOutline scenarioOutline, ParameterSubstitution paramToIdentifier)
        {
            var testMethod = _codeDomHelper.CreateMethod(generationContext.TestClass);

            testMethod.ReturnType = new CodeTypeReference(typeof(Task));
            testMethod.Attributes = MemberAttributes.Public;
            testMethod.Name       = string.Format(GeneratorConstants.TEST_NAME_FORMAT, scenarioOutline.Name.ToIdentifier());

            _unitTestGeneratorProvider.MarkCodeMemberMethodAsAsync(testMethod);

            foreach (var pair in paramToIdentifier)
            {
                testMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), pair.Value));
            }

            testMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string[]), GeneratorConstants.SCENARIO_OUTLINE_EXAMPLE_TAGS_PARAMETER));
            return(testMethod);
        }