Skip to content

Commit

Permalink
More descriptive name for the scenario outline example tests than XY…
Browse files Browse the repository at this point in the history
…Z_Variant1 (Issue 18)
  • Loading branch information
gasparnagy committed Nov 10, 2009
1 parent 8341bd3 commit 304f988
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 20 deletions.
20 changes: 17 additions & 3 deletions Parser/SpecFlowUnitTestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<KeyValuePair<string, string>> paramToIdentifier, string exampleSetTitle, Row row, int rowIndex)
private void GenerateScenarioOutlineTestVariant(CodeTypeDeclaration testType, ScenarioOutline scenarioOutline, string testMethodName, List<KeyValuePair<string, string>> 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<CodeExpression> argumentExpressions = new List<CodeExpression>();
Expand Down
5 changes: 5 additions & 0 deletions Tests/ParserTests/TestFiles/scenariooutline.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
46 changes: 46 additions & 0 deletions Tests/ParserTests/TestFiles/scenariooutline.feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,52 @@
</Body>
</Table>
</ExampleSet>
<ExampleSet>
<Title>last example set with non-unique first column</Title>
<Table>
<Header>
<Cells>
<Cell>
<Value>templated</Value>
</Cell>
<Cell>
<Value>date1</Value>
</Cell>
<Cell>
<Value>date2</Value>
</Cell>
</Cells>
</Header>
<Body>
<Row>
<Cells>
<Cell>
<Value>five</Value>
</Cell>
<Cell>
<Value>2009/09/14</Value>
</Cell>
<Cell>
<Value>2009/09/14</Value>
</Cell>
</Cells>
</Row>
<Row>
<Cells>
<Cell>
<Value>five</Value>
</Cell>
<Cell>
<Value>2009/09/15</Value>
</Cell>
<Cell>
<Value>2009/09/15</Value>
</Cell>
</Cells>
</Row>
</Body>
</Table>
</ExampleSet>
</ExampleSets>
</Examples>
</Scenario>
Expand Down
23 changes: 17 additions & 6 deletions Tests/RuntimeTests/NUnitTestExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
Expand All @@ -8,20 +9,30 @@ namespace TechTalk.SpecFlow.RuntimeTests
{
class NUnitTestExecutor
{
public static void ExecuteNUnitTests(object test)
public static void ExecuteNUnitTests(object test, Func<Exception, bool> 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
Expand Down
18 changes: 8 additions & 10 deletions Tests/RuntimeTests/SuccessfulExecutionTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System;
using System.Reflection;
using NUnit.Framework;
using TechTalk.SpecFlow.Parser.SyntaxElements;

Expand All @@ -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;
});
}
}
}
2 changes: 1 addition & 1 deletion Tests/RuntimeTests/ValidateStepAndEventOrdersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 304f988

Please sign in to comment.