Skip to content
This repository has been archived by the owner on Jun 12, 2021. It is now read-only.

support nested context (steps) #251

Closed
wangvnn opened this issue May 1, 2015 · 1 comment
Closed

support nested context (steps) #251

wangvnn opened this issue May 1, 2015 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@wangvnn
Copy link

wangvnn commented May 1, 2015

I am looking for BDD test framework that is based on xUnit.
xBehave looks like a good one with nice syntax.

Below is a wishful thought, it may not be a good idea.
Note: nested given steps, and nested when steps.
This makes use of the C# closure to reuse/remove local variables from other steps.
I find it is a huge speed up in term of writing test. On the downside, readability may be not as good as writing one when step per scenario. But this can be considered as a trade-off.

In some cases, nested context is very useful and straight forward to write test without thinking about organizing reuse component. In the below example I just explore more and more cases (branches) of the scenarios. I don't have to think about separate common logic to reuse them. It just naturally grow at will.

public class TestCalc
{
    [Scenario]
    public void TestAdd()
    {
        "Given the number 1".f(() =>
        {
            int x = 1;
            "And the number 2".f(() =>
            {
                int y = 2;
                "And a calculator".f(() =>
                {
                    var calculator = new Calculator();
                    "When I add the numbers together".When(() =>
                    {
                        var answer = calculator.Add(x, y);
                        "Then the answer is 3".f(() =>
                        {
                            answer.ShouldBeEqualTo(3);
                        });
                        "When I add  3 to the answer".When(() =>
                        {
                            answer = calculator.Add(answer, 3);
                            "Then the answer is 6".f(() =>
                            {
                                answer.ShouldBeEqualTo(6);
                            });
                        });
                        "When I add  2 to the answer".When(() =>
                        {
                            answer = calculator.Add(answer, 2);
                            "Then the answer is 5".f(() =>
                            {
                                answer.ShouldBeEqualTo(5);
                            });
                        });
                    });
                });
            });
        });
    }
}

PS:
I tried Catch for c++, and it works well for nested context.
https://github.com/philsquared/Catch/blob/master/docs/tutorial.md

NJasmine also works for nested context. But it is based on NUnit.
https://github.com/fschwiet/DreamNJasmine

It has an issue though: in its running test process, there are 2 steps:

  1. Test discovery: at this step, it actually runs the steps to discover the steps
    ( for given, and when steps) which will end up with running test twice.
    And it runs the steps without running the beforeAll, so if you have dependency between steps and beforeAll, the steps will fail.

  2. Test running: run the steps (given, when, and then)

@adamralph adamralph added the duplicate This issue or pull request already exists label May 1, 2015
@adamralph
Copy link
Owner

Hi, @wangvnn thanks for raising this. We've already discussed the notion of nested contexts/steps over at #98 so I'm going to close this as a duplicate, but it's good to see your proposed use case. I'm sure we can come up with something which works as a post-2.0 feature.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants