/NScenario

Dead simple library for annotating steps of test case scenarios.

Primary LanguageC#MIT LicenseMIT

NScenario

Dead simple, test framework independent, without any magic, a library for annotating steps of test case scenarios.

How to install

NScenario is distribute as a nuget package NScenario

How to use it

Just create an instance of NScenario.TestScenario class and start annotating your test steps by wrapping it in Step method call. You can create TestScenario instance manually by providing a desired composition of IScenarioStepExecutor instances or simply by calling TestScenarioFactory.Default() method.

Example test can look as follows:

[Test]
public async Task should_activate_community_supporter_license_when_eligible()
{
    using var driver = await LicenseServerTestDriver.Create();
    var scenario = TestScenarioFactory.Default();

    var activationData = new
    {
        email = "abs@test.com",
        licenseKey = "WTKP4-66NL5-HMKQW-GFSCZ"
    };

    await scenario.Step("Import supporters", async () =>
    {
        await driver.ImportSupporters("BuyMeCoffee", new[] { "john@done.com", "jane@done.com", activationData.email });
    });

    await scenario.Step("Register purchase for supporter email", async () =>
    {
        await driver.RegisterPurchaseWithCoupon("Extension for VS2017", activationData.email, activationData.licenseKey, "OssSupporter");
    });

    await scenario.Step("Activate the license with supporter email", async () =>
    {
        var activationResult = await scenario.Step("Call active license endpoint" () => 
        {
            return await driver.ActivateLicense(activationData.email, activationData.licenseKey);
        });
        
        await scenario.Step("Verify that license activated properly", () =>
        {
            Assert.AreEqual(true, activationResult.Activated);
            Assert.AreEqual("Unlimited", activationResult.Capabilities["VsVersion"]);
        });
    });
}

Console output

SCENARIO: should activate community supporter license when eligible

STEP 1: Import supporters
STEP 2: Register purchase for supporter email
STEP 3: Activate the license with supporter email
    STEP 3.1: Call active license endpoint
    STEP 3.2: Verify that license activated properly

Benefits:

  • Obvious way to enforce step descriptions
  • More readable test scenario
  • Sub-scopes for repeatable steps
  • Readable output that facilitates broken scenario investigation

Console output

Some test runners are hijacking console output and provide a custom stream for logging. By default NScenario is writing scenario description to the console, but this can be overridden by providing a custom TextWriter stream to TestScenarioFactory.Default() method. For example, in case of problems with NUnit you can initialize TestScenario as follows:

var scenario = TestScenarioFactory.Default(TestContext.Out);

Test scenario description

Test scenario description is generated by removing underscores from the test method name ([CallerMemberName] is used to retrieve that name). This allows for immediate review of the test name (I saw many, extremely long and totally ridiculous test method names. A good test method name should reveal the intention of the test case, not its details).

NScenario is prefixing scenario title with SCENARIO: prefix and every step is prefixed with STEP. If you are writing step descriptions in other languages than English, you can override those prefixes by specifing them explicitly why calling TestScenarioFactory.Default() method.

var scenario = TestScenarioFactory.Default(scenarioPrefix: "SCENARIUSZ", stepPrefix: "KROK");

Why not XBehave.net

xBehave.net is the XUnit extension so it can be used only with XUnit based tests. In my opinion, it is also quite cryptic (string extension methods called with single letter might not obvious) and a little bit over-complicated. BUT THIS IS MY PERSONAL OPINION