How to use before_all method of NSpec.Tests.WhenRunningSpecs.BeforeAndAfter.SpecClass class

Best NSpec code snippet using NSpec.Tests.WhenRunningSpecs.BeforeAndAfter.SpecClass.before_all

Run NSpec automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

class_levels.cs

Source: class_levels.cs Github

copy
1using FluentAssertions;
2using NUnit.Framework;
3
4namespace NSpec.Tests.WhenRunningSpecs.BeforeAndAfter
5{
6    [TestFixture]
7    [Category("RunningSpecs")]
8    public class class_levels : when_running_specs
9    {
10        class SpecClass : sequence_spec
11        {
12            void before_all()
13            {
14                sequence = "A";
15            }
16
17            void before_each()
18            {
19                sequence += "B";
20            }
21
22            void it_one_is_one()
23            {
24                sequence += "1";
25            }
26
27            void it_two_is_two()
28            {
29                sequence += "2"; //two specs cause before_each and after_each to run twice
30            }
31
32            void after_each()
33            {
34                sequence += "C";
35            }
36
37            void after_all()
38            {
39                sequence += "D";
40            }
41        }
42
43        [Test]
44        public void everything_runs_in_the_correct_order()
45        {
46            Run(typeof(SpecClass));
47
48            SpecClass.sequence.Should().Be("AB1CB2CD");
49        }
50    }
51}
Full Screen

class_levels_and_context_methods.cs

Source: class_levels_and_context_methods.cs Github

copy
1using FluentAssertions;
2using NUnit.Framework;
3
4namespace NSpec.Tests.WhenRunningSpecs.BeforeAndAfter
5{
6    [TestFixture]
7    [Category("RunningSpecs")]
8    public class class_levels_and_context_methods : when_running_specs
9    {
10        class SpecClass : sequence_spec
11        {
12            void before_all()
13            {
14                sequence = "A";
15            }
16
17            void before_each()
18            {
19                sequence += "C";
20            }
21
22            void a_context()
23            {
24                beforeAll = () => sequence += "B";
25
26                before = () => sequence += "D";
27                specify = () => Assert.That(true, Is.True);
28                after = () => sequence += "E";
29
30                afterAll = () => sequence += "G";
31            }
32
33            void after_each()
34            {
35                sequence += "F";
36            }
37
38            void after_all()
39            {
40                sequence += "H";
41            }
42        }
43
44        [SetUp]
45        public void setup()
46        {
47            Run(typeof(SpecClass));
48        }
49
50        [Test]
51        public void before_alls_at_every_level_run_before_before_eaches_from_the_outside_in()
52        {
53            SpecClass.sequence.Should().StartWith("ABCD");
54        }
55
56        [Test]
57        public void after_alls_at_every_level_run_after_after_eaches_from_the_inside_out()
58        {
59            SpecClass.sequence.Should().EndWith("EFGH");
60        }
61    }
62}
63
Full Screen

async_class_levels.cs

Source: async_class_levels.cs Github

copy
1using FluentAssertions;
2using NUnit.Framework;
3using System.Threading.Tasks;
4
5namespace NSpec.Tests.WhenRunningSpecs.BeforeAndAfter
6{
7    [TestFixture]
8    [Category("RunningSpecs")]
9    [Category("Async")]
10    public class async_class_levels : when_running_specs
11    {
12        class SpecClass : sequence_spec
13        {
14            async Task before_all()
15            {
16                await Task.Run(() => sequence = "A");
17            }
18
19            async Task before_each()
20            {
21                await Task.Run(() => sequence += "B");
22            }
23
24            async Task it_one_is_one()
25            {
26                await Task.Run(() => sequence += "1");
27            }
28
29            async Task it_two_is_two()
30            {
31                await Task.Run(() => sequence += "2"); //two specs cause before_each and after_each to run twice
32            }
33
34            async Task after_each()
35            {
36                await Task.Run(() => sequence += "C");
37            }
38
39            async Task after_all()
40            {
41                await Task.Run(() => sequence += "D");
42            }
43        }
44
45        [Test]
46        public void everything_runs_in_the_correct_order()
47        {
48            Run(typeof(SpecClass));
49
50            SpecClass.sequence.Should().Be("AB1CB2CD");
51        }
52    }
53}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)