- Notifications
You must be signed in to change notification settings - Fork5
Dead simple library for annotating steps of test case scenarios.
License
cezarypiatek/NScenario
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Dead simple, test framework independent, without any magic, a library for annotating steps of test case scenarios.Please readReadable and clear tests for ASP.NET Core services article for better exaplanation and example use cases.
This library was discussed duringASP.NET Community Standup 2021-08-24
NScenario is distribute as a nuget packageNScenario
Just create an instance ofNScenario.TestScenario class and start annotating your test steps by wrapping it inStep method call.You can createTestScenario instance manually by providing a desired composition ofIScenarioStepExecutor instances or simply by callingTestScenarioFactory.Default() method.
Example test can look as follows:
[Test]publicasyncTaskshould_activate_community_supporter_license_when_eligible(){usingvardriver=awaitLicenseServerTestDriver.Create();varscenario=TestScenarioFactory.Default();varactivationData=new{email="abs@test.com",licenseKey="WTKP4-66NL5-HMKQW-GFSCZ"};awaitscenario.Step("Import supporters",async()=>{awaitdriver.ImportSupporters("BuyMeCoffee",new[]{"john@done.com","jane@done.com",activationData.email});});awaitscenario.Step("Register purchase for supporter email",async()=>{awaitdriver.RegisterPurchaseWithCoupon("Extension for VS2017",activationData.email,activationData.licenseKey,"OssSupporter");});awaitscenario.Step("Activate the license with supporter email",async()=>{varactivationResult=awaitscenario.Step("Call active license endpoint"()=>{returnawait driver.ActivateLicense(activationData.email,activationData.licenseKey);});awaitscenario.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 eligibleSTEP 1: Import supportersSTEP 2: Register purchase for supporter emailSTEP 3: Activate the license with supporter email STEP 3.1: Call active license endpoint STEP 3.2: Verify that license activated properlyBenefits:
- Obvious way to enforce step descriptions
- More readable test scenario
- Sub-scopes for repeatable steps
- Readable output that facilitates broken scenario investigation
Some test runners are hijacking console output and provide a custom stream for logging. By defaultNScenario is writing scenario description to the console, but this can be overridden by providing a customTextWriter stream toTestScenarioFactory.Default() method.
publicclassMyTests{[Test]publicvoidsample_test_case(){varscenario=TestScenarioFactory.Default(TestContext.Progress);}}
publicclassXUnitOutputAdapter:TextWriter{privatereadonlyITestOutputHelper_output;publicXUnitOutputAdapter(ITestOutputHelperoutput)=>_output=output;publicoverridevoidWriteLine(string?value)=>_output.WriteLine(value);publicoverrideEncodingEncoding{get;}}publicclassMyTests{privatereadonlyITestOutputHelper_output;publicMyTests(ITestOutputHelperoutput)=>this._output=output;[Fact]publicvoidsample_test_case(){varscenario=TestScenarioFactory.Default(newXUnitOutputAdapter(_output));}}
More info aboutcapturing console output in XUnit
Test scenario output can be configured globally by settingTestScenarioFactory.DefaultScenarioOutputWriter.
Example usingModule initializer :
usingNScenario.OutputWriters;usingNScenario.StepExecutors;publicstaticclassGlobalSetup{[System.Runtime.CompilerServices.ModuleInitializer]publicstaticvoidSetup(){TestScenarioFactory.DefaultScenarioOutputWriter=newStreamScenarioOutputWriter(TestContext.Progress);}}
Example usingSetUpFixture forNUnit
You should put that code under the default namespace:
usingNScenario.OutputWriters;usingNScenario.StepExecutors;usingNUnit.Framework;[SetUpFixture]publicclassAllTestsSetup{[OneTimeSetUp]publicvoidGlobalSetup(){TestScenarioFactory.DefaultScenarioOutputWriter=newStreamScenarioOutputWriter(TestContext.Progress);}}
You save the test scenario transcription as Markdown (with option to export as HTML) usingMarkdownFormatterOutputWriter.
Sample setup with exporting scenario transcription to a file:
usingNScenario.OutputWriters;usingNScenario.StepExecutors;usingNUnit.Framework;[SetUpFixture]publicclassAllTestsSetup{privatereadonlyMarkdownFormatterOutputWriter_reportWriter=new(title:"Sample tests with NScenario",currentTestIdentifierAccessor:()=>TestContext.CurrentContext.Test.ID);[OneTimeSetUp]publicvoidGlobalSetup(){TestScenarioFactory.DefaultScenarioOutputWriter=newComposeScenarioOutputWriter(newIScenarioOutputWriter[]{//INFO: Configure live reporting to console with NUnitnewStreamScenarioOutputWriter(TestContext.Progress),//INFO: Configure collecting transcription as markdown_reportWriter});}[OneTimeTearDown]publicvoidGlobalTearDown(){// INFO: Save the raw Markdown to a file_reportWriter.Save("Report.md");//INFO: Export the markdown to HTML file_reportWriter.ExportToHtml("Report.html");}}
There's also an option to generate a nice html report for all test scenarios. Just invokeTestScenarioFactory.GetAllExecutedScenarios().SaveAsReport("AllReports.html"); in your global teardown to get a report like the one below:
This report browser supports links to scenario steps definition. To make it work, you need to set the following msbuild properties (or environment variables):
- RepositoryUrl
- SourceRevisionId
Test scenario title is generated by removing underscores and splitting camel/pascalcase string 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). You can always override the default title by setting it explicitly during test scenario creation (especially useful for parametrized test methods):
[TestCase(false)][TestCase(true)]publicasyncTaskshould_present_basic_scenario_with_explicit_title(boolsomeFlag){varscenario=TestScenarioFactory.Default(title:$"some scenario when flag set to '{someFlag}'");awaitscenario.Step("This is the first step",()=>{// Here comes the logic});awaitscenario.Step("This is the second step",()=>{// Here comes the logic});awaitscenario.Step("This is the third step",()=>{// Here comes the logic});}
NScenario is prefixing scenario title withSCENARIO: prefix and every step is prefixed withSTEP. If you are writing step descriptions in other languages than English, you can override those prefixes by specifing them explicitly why callingTestScenarioFactory.Default() method.
varscenario=TestScenarioFactory.Default(scenarioPrefix:"SCENARIUSZ",stepPrefix:"KROK");
xBehave.net is theXUnit 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
About
Dead simple library for annotating steps of test case scenarios.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Contributors5
Uh oh!
There was an error while loading.Please reload this page.
