{"id":738,"date":"2009-02-28T16:46:04","date_gmt":"2009-02-28T15:46:04","guid":{"rendered":"http:\/\/www.navision-blog.de\/2009\/02\/28\/parameterized-scenarios-with-naturalspec\/"},"modified":"2023-01-03T13:40:03","modified_gmt":"2023-01-03T13:40:03","slug":"parameterized-scenarios-with-naturalspec","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2009\/02\/28\/parameterized-scenarios-with-naturalspec\/","title":{"rendered":"Parameterized Scenarios with NaturalSpec"},"content":{"rendered":"<p>I wrote a lot about <a href=\"http:\/\/code.google.com\/p\/natural\/\">NaturalSpec<\/a> in my <a href=\"http:\/\/www.navision-blog.de\/tag\/naturalspec\/\">last articles<\/a>. This time I will show how we can use parameterized scenarios.<\/p>\n<h5>1. Using predefined scenarios<\/h5>\n<p>By writing predefined parameterized scenarios we can easily create a scenario suite with lots of different test cases:<\/p>\n<pre class=\"code\"><span style=\"color: green\">\/\/ predefined scenario\n<\/span><span style=\"color: blue\">let <\/span>factorialScenario x result =\n  Given x\n    |&gt; When calculating factorial\n    |&gt; It should equal result\n\n[&lt;Scenario&gt;]\n<span style=\"color: blue\">let <\/span>When_calculation_factorial_of_1() =\n  factorialScenario 1 1\n    |&gt; Verify   \n\n[&lt;Scenario&gt;]\n<span style=\"color: blue\">let <\/span>When_calculation_factorial_of_10() =\n  factorialScenario 10 3628800\n    |&gt; Verify<\/pre>\n<p>If we run these scenarios with <a href=\"http:\/\/nunit.com\/index.php\">NUnit<\/a> we will get the following output:<\/p>\n<blockquote>\n<p>Scenario: When calculation factorial of 1<\/p>\n<p>&#160; &#8211; Given 1 <\/p>\n<p>&#160;&#160;&#160; &#8211; When calculating factorial <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160; =&gt; It should equal 1<\/p>\n<p>==&gt; OK<\/p>\n<p>==&gt; Time: 0.0093s<\/p>\n<p>Scenario: When calculation factorial of 10<\/p>\n<p>&#160; &#8211; Given 10 <\/p>\n<p>&#160;&#160;&#160; &#8211; When calculating factorial <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160; =&gt; It should equal 3628800<\/p>\n<p>==&gt; OK<\/p>\n<p>==&gt; Time: 0.0018s<\/p>\n<\/blockquote>\n<h5>2. Using the ScenarioTemplate attribute<\/h5>\n<p>The second and shorter option is to use the <em>ScenarioTemplate<\/em> attribute, which is inherited from NUnit\u2019s <a href=\"http:\/\/nunit.com\/blogs\/?p=60\">new <em>TestCase<\/em> attribute<\/a>:<\/p>\n<pre class=\"code\"><span style=\"color: green\">\/\/ with ScenarioTemplate Attribute\n<\/span>[&lt;ScenarioTemplate(1, 1)&gt;]\n[&lt;ScenarioTemplate(2, 2)&gt;]\n[&lt;ScenarioTemplate(5, 120)&gt;]\n[&lt;ScenarioTemplate(10, 3628800)&gt;]\n<span style=\"color: blue\">let <\/span>When_calculating_fac_(x,result) =\n  Given x\n    |&gt; When calculating factorial\n    |&gt; It should equal result\n    |&gt; Verify<\/pre>\n<p>This code will create 4 different scenarios, which NUnit will display and report separately:<\/p>\n<p><img class=\"bordered\" title=\"NUnit runner reports test cases\" alt=\"NUnit runner reports test cases\" src=\"http:\/\/www.navision-blog.de\/images\/ParameterizedScenarioswithNaturalSpec_EBB7\/image.png\" width=\"271\" border=\"0\" \/><\/p>\n<h5>3. Using ScenarioSource<\/h5>\n<p>The third option is to use the <em>ScenarioSource<\/em> attribute. Here we define a function which generates TestData:<\/p>\n<pre class=\"code\"><span style=\"color: green\">\/\/\/ with a scenario source      \n<\/span><span style=\"color: blue\">let <\/span>MyTestCases =\n  TestWith 12 3 4\n    |&gt; And 12 4 3\n    |&gt; And 12 6 2\n    |&gt; And 1200 40 30\n    |&gt; And 0 0 0 |&gt; ShouldFailWith (typeof&lt;System.DivideByZeroException&gt;)<\/pre>\n<p>And then we have to tell NaturalSpec which TestData a scenario should use:<\/p>\n<pre class=\"code\">[&lt;Scenario&gt;]\n[&lt;ScenarioSource <span style=\"color: maroon\">&quot;MyTestCases&quot;<\/span>&gt;]\n<span style=\"color: blue\">let <\/span>When_dividing a b result =\n Given a \n   |&gt; When dividing_by b\n   |&gt; It should equal result\n   |&gt; Verify<\/pre>\n<pre class=\"code\">&#160;<img class=\"bordered\" title=\"image\" alt=\"NaturalSpec with ScenarioSource\" src=\"http:\/\/www.navision-blog.de\/images\/ParameterizedScenarioswithNaturalSpec_EBB7\/image_3.png\" width=\"500\" border=\"0\" \/>  <\/pre>\n<h5>Summary<\/h5>\n<p>Sometime it makes sense to test a scenario with a bunch of different parameters. We can use the <em>ScenarioTemplate<\/em> attribute to easily parameterize our scenarios. If we want more flexibility we can use predefined scenarios with custom parameters or the ScenarioSource attribute.<\/p>\n<div style=\"font-size:0px;\"><a href=\"https:\/\/247apotheek.com\">https:\/\/247apotheek.com<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I wrote a lot about NaturalSpec in my last articles. This time I will show how we can use parameterized scenarios. 1. Using predefined scenarios By writing predefined parameterized scenarios we can easily create a scenario suite with lots of different test cases: \/\/ predefined scenario let factorialScenario x result = Given x |&gt; When [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[448,524],"tags":[664,666,219,538,531],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/738"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=738"}],"version-history":[{"count":6,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/738\/revisions"}],"predecessor-version":[{"id":2052,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/738\/revisions\/2052"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=738"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}