From 304f988861467ec0a1e9c164b161a2b9c8b301ed Mon Sep 17 00:00:00 2001 From: Gaspar Nagy Date: Tue, 10 Nov 2009 17:09:12 +0100 Subject: [PATCH] =?UTF-8?q?=EF=BB=BFMore=20descriptive=20name=20for=20the?= =?UTF-8?q?=20scenario=20outline=20example=20tests=20than=20XYZ=5FVariant1?= =?UTF-8?q?=20(Issue=2018)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Parser/SpecFlowUnitTestConverter.cs | 20 ++++++-- .../TestFiles/scenariooutline.feature | 5 ++ .../TestFiles/scenariooutline.feature.xml | 46 +++++++++++++++++++ Tests/RuntimeTests/NUnitTestExecutor.cs | 23 +++++++--- Tests/RuntimeTests/SuccessfulExecutionTest.cs | 18 ++++---- .../ValidateStepAndEventOrdersTest.cs | 2 +- changelog.txt | 1 + 7 files changed, 95 insertions(+), 20 deletions(-) diff --git a/Parser/SpecFlowUnitTestConverter.cs b/Parser/SpecFlowUnitTestConverter.cs index d0831936b..973f49d19 100644 --- a/Parser/SpecFlowUnitTestConverter.cs +++ b/Parser/SpecFlowUnitTestConverter.cs @@ -291,14 +291,26 @@ private void GenerateScenarioOutlineTest(CodeTypeDeclaration testType, CodeMembe string exampleSetTitle = exampleSet.Title == null ? string.Format("Scenarios{0}", exampleSetIndex + 1) : exampleSet.Title.ToIdentifier(); + bool useFirstColumnAsName = CanUseFirstColumnAsName(exampleSet.Table); + for (int rowIndex = 0; rowIndex < exampleSet.Table.Body.Length; rowIndex++) { - GenerateScenarioOutlineTestVariant(testType, scenarioOutline, testMethodName, paramToIdentifier, exampleSetTitle, exampleSet.Table.Body[rowIndex], rowIndex); + string variantName = useFirstColumnAsName ? exampleSet.Table.Body[rowIndex].Cells[0].Value.ToIdentifier() : + string.Format("Variant{0}", rowIndex); + GenerateScenarioOutlineTestVariant(testType, scenarioOutline, testMethodName, paramToIdentifier, exampleSetTitle, exampleSet.Table.Body[rowIndex], variantName); } exampleSetIndex++; } } + private bool CanUseFirstColumnAsName(Table table) + { + if (table.Header.Cells.Length == 0) + return false; + + return table.Body.Select(r => r.Cells[0].Value.ToIdentifier()).Distinct().Count() == table.Body.Length; + } + private void GenerateScenarioOutlineBody(ScenarioOutline scenarioOutline, ParameterSubstitution paramToIdentifier, CodeTypeDeclaration testType, string testMethodName, CodeMemberMethod testSetup) { CodeMemberMethod testMethod = new CodeMemberMethod(); @@ -315,10 +327,12 @@ private void GenerateScenarioOutlineBody(ScenarioOutline scenarioOutline, Parame GenerateTestBody(scenarioOutline, testMethod, testSetup, paramToIdentifier); } - private void GenerateScenarioOutlineTestVariant(CodeTypeDeclaration testType, ScenarioOutline scenarioOutline, string testMethodName, List> paramToIdentifier, string exampleSetTitle, Row row, int rowIndex) + private void GenerateScenarioOutlineTestVariant(CodeTypeDeclaration testType, ScenarioOutline scenarioOutline, string testMethodName, List> paramToIdentifier, string exampleSetTitle, Row row, string variantName) { CodeMemberMethod testMethod = GetTestMethodDeclaration(testType, scenarioOutline); - testMethod.Name = string.Format("{0}_{1}_Variant{2}", testMethod.Name, exampleSetTitle, rowIndex); + testMethod.Name = string.IsNullOrEmpty(exampleSetTitle) ? + string.Format("{0}_{1}", testMethod.Name, variantName) : + string.Format("{0}_{1}_{2}", testMethod.Name, exampleSetTitle, variantName); //call test implementation with the params List argumentExpressions = new List(); diff --git a/Tests/ParserTests/TestFiles/scenariooutline.feature b/Tests/ParserTests/TestFiles/scenariooutline.feature index 343f8ebaa..e6e46774b 100644 --- a/Tests/ParserTests/TestFiles/scenariooutline.feature +++ b/Tests/ParserTests/TestFiles/scenariooutline.feature @@ -21,3 +21,8 @@ Scenarios: second example set Scenarios: | templated | date1 | date2 | | four | 2009/09/14 | 2009/09/14 | + +Examples: last example set with non-unique first column + | templated | date1 | date2 | + | five | 2009/09/14 | 2009/09/14 | + | five | 2009/09/15 | 2009/09/15 | diff --git a/Tests/ParserTests/TestFiles/scenariooutline.feature.xml b/Tests/ParserTests/TestFiles/scenariooutline.feature.xml index 5392ed2b9..45e83cfd7 100644 --- a/Tests/ParserTests/TestFiles/scenariooutline.feature.xml +++ b/Tests/ParserTests/TestFiles/scenariooutline.feature.xml @@ -185,6 +185,52 @@ + + last example set with non-unique first column + +
+ + + templated + + + date1 + + + date2 + + +
+ + + + + five + + + 2009/09/14 + + + 2009/09/14 + + + + + + + five + + + 2009/09/15 + + + 2009/09/15 + + + + +
+
diff --git a/Tests/RuntimeTests/NUnitTestExecutor.cs b/Tests/RuntimeTests/NUnitTestExecutor.cs index 343c462ec..5d53263d2 100644 --- a/Tests/RuntimeTests/NUnitTestExecutor.cs +++ b/Tests/RuntimeTests/NUnitTestExecutor.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Reflection; using NUnit.Framework; @@ -8,20 +9,30 @@ namespace TechTalk.SpecFlow.RuntimeTests { class NUnitTestExecutor { - public static void ExecuteNUnitTests(object test) + public static void ExecuteNUnitTests(object test, Func onError) { // fixture setup ExecuteWithAttribute(test, typeof(TestFixtureSetUpAttribute)); foreach (var testMethod in GetMethodsWithAttribute(test, typeof(TestAttribute))) { - // test setup - ExecuteWithAttribute(test, typeof(SetUpAttribute)); + try + { + Debug.WriteLine(testMethod, "Executing test"); - InvokeMethod(test, testMethod); + // test setup + ExecuteWithAttribute(test, typeof(SetUpAttribute)); - // test teardown - ExecuteWithAttribute(test, typeof(TearDownAttribute)); + InvokeMethod(test, testMethod); + + // test teardown + ExecuteWithAttribute(test, typeof(TearDownAttribute)); + } + catch(Exception ex) + { + if (onError == null || !onError(ex)) + throw; + } } // fixture teardown diff --git a/Tests/RuntimeTests/SuccessfulExecutionTest.cs b/Tests/RuntimeTests/SuccessfulExecutionTest.cs index 19c1859bf..a6b0ef327 100644 --- a/Tests/RuntimeTests/SuccessfulExecutionTest.cs +++ b/Tests/RuntimeTests/SuccessfulExecutionTest.cs @@ -1,4 +1,5 @@ -using System.Reflection; +using System; +using System.Reflection; using NUnit.Framework; using TechTalk.SpecFlow.Parser.SyntaxElements; @@ -17,15 +18,12 @@ public void FixtureSetup() protected override void ExecuteTests(object test, Feature feature) { - try - { - NUnitTestExecutor.ExecuteNUnitTests(test); - Assert.Fail("incloncusive exception expected"); - } - catch(InconclusiveException) - { - - } + NUnitTestExecutor.ExecuteNUnitTests(test, + delegate(Exception exception) + { + Assert.IsInstanceOf(typeof(InconclusiveException), exception); + return true; + }); } } } diff --git a/Tests/RuntimeTests/ValidateStepAndEventOrdersTest.cs b/Tests/RuntimeTests/ValidateStepAndEventOrdersTest.cs index 0baf91064..6d7c2e75b 100644 --- a/Tests/RuntimeTests/ValidateStepAndEventOrdersTest.cs +++ b/Tests/RuntimeTests/ValidateStepAndEventOrdersTest.cs @@ -16,7 +16,7 @@ protected override void ExecuteTests(object test, Feature feature) { MockRepository mockRepository = SetupTests(feature); - NUnitTestExecutor.ExecuteNUnitTests(test); + NUnitTestExecutor.ExecuteNUnitTests(test, ex => true); mockRepository.VerifyAll(); } diff --git a/changelog.txt b/changelog.txt index f6681d190..dd4dd2ba9 100644 --- a/changelog.txt +++ b/changelog.txt @@ -7,6 +7,7 @@ New features: + Support German, French and Hungarian languages (Issue 5) + Add strong-name for specflow assemblies (Issue 2) + Allow scenario events to be instance methods (Issue 20) ++ More descriptive name for the scenario outline example tests than XYZ_Variant1 (Issue 18) Fixed issues: + Runtime: Remove direct dependency on nunit.framework.dll from the runtime (Issue 12)