Skip to content

TearDown method not being invoked after exception thrown in Then step #243

@TAGC

Description

@TAGC

I have a BDDfy test set up as follows:

public MofiAnalysesPhrase() : base("Mofichan analyses a phrase given to her")
{
	var command = default(string);
	var tags = default(IEnumerable<string>);

	this.Given(s => s.Given_Mofichan_is_configured_with_behaviour("analysis"))
			.And(s => s.Given_Mofichan_is_running())
		.When(s => s.When_Mofichan_receives_a_message(this.JohnSmithUser, command))
			.And(s => s.When_behaviours_are_driven_by__pulseCount__pulses(2))
		.Then(s => s.Then_Mofichan_should_have_responded_with_expected_analysis(tags))
		.WithExamples(new ExampleTable("command", "tags")
		{
			{ "Mofi, perform analysis \"hello Mofi\"", new[] { "directedAtMofichan", "greeting" } },
			{ "Mofi, perform analysis: \"how are you doing?\"", new[] { "wellbeing" } },
			{ "Mofi, analyse: \"you're the best!\"", new[] { "positive" } },
			{ "Mofi, analyse phrase: \"you're the worst!\"", new[] { "negative" } },
			{ "Mofi, analyse phrase: \"That's right Mofi\"", new[] { "directedAtMofichan", "affirmation" } },
			{ "Mofi, analyse phrase: \"That's correct Mofi\"", new[] { "directedAtMofichan", "affirmation" } },
			{ "Mofi, analyse phrase: \"That's wrong Mofi\"", new[] { "directedAtMofichan", "refutation" } },
			{ "Mofi, analyse phrase: \"That's incorrect Mofichan\"", new[] { "directedAtMofichan", "refutation" } },
		})
		.TearDownWith(s => s.TearDown());
}

This BDDfy test is configured to run for a set of example data. Currently, some of these examples pass and some fail.

Up until the first failure, the tests work as expected - the given, when and then steps are performed in order, followed by the teardown using TearDown(). However, as soon as the first test failure occurs (signified by an exception being raised within Then_Mofichan_should_have_responded_with_expected_analysis), the teardown method stops being invoked, meaning that all following tests fail due to the test context not being "clean" as opposed to the actual code under test being wrong. By stepping through the code using a debugger, I can see that as soon as an exception is thrown within the Then step, the test runner invokes the Given_Mofichan_is_configured_with_behaviour without going through TearDown first.

I get the following output when running these tests:

BDDfy output

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions