D E V M A T E

Loading

Boilerplates

You are here:
Estimated reading time: 1 min

Create a boilerplate code for unit-testing

For a creation of a simple example solution please refer to Chapter 2b. We use same a .Net Core library project and a static method Greet(string whom):

namespace HelloWorld
{
        public static class Example
        {
            public static string Greet(string whom)
            {
                return $"Hello {whom}";
            }      
    }
}

To create a boilerplate code for this method, right-click inside the method and select “Create Boilerplate with devmate” from the context menu and choose a unit-testing framework:

image2021-4-20_13-44-4.png

A file dialog will ask you where to store the generated boilerplate code. Store it somewhere in your test project:

image2020-10-14_13-48-23.png

That’s it. You just created your first boilerplate code using devmate:

using System;
using System.Collections.Generic;
using NUnit.Framework;
 
namespace HelloWorldTests
{
    public class GreetTestCase
    {
#region Test methods
        [Test]
        [TestCaseSource(nameof(ExpectedReturnValueTests))]
        public void GreetTest(ExpectedValueTestData<string> data)
        {
            var actual = HelloWorld.Example.Greet(data.Params.whom);
            Assert.AreEqual(data.ExpectedValue, actual);
        }
 
        [Test]
        [TestCaseSource(nameof(TestsThrowingException))]
        public void GreetTestThrowingException(TestThrowingExceptionData data)
        {
            Assert.Throws(data.ExpectedException, () => HelloWorld.Example.Greet(data.Params.whom));
        }
#endregion
 
#region Data
        private static IEnumerable<TestCaseData> ExpectedReturnValueTests()
        {
            yield return new TestCaseData(
                new ExpectedValueTestData<string>
                {
                    Params = new Parameters
                    {
                        whom = /* TODO: Replace 'default' by your expected value */ default,
                    },
                    ExpectedValue = /* TODO: Replace 'default' by your expected value */ default,
                }).SetName(@"set your test name here - set your test description here");
        }
 
        private static IEnumerable<TestCaseData> TestsThrowingException()
        {
            yield return new TestCaseData(
                new TestThrowingExceptionData
                {
                    Params = new Parameters
                    {
                        whom = /* TODO: Replace 'default' by your expected value */ default,
                    },
                    ExpectedException = /* TODO: Replace 'Exception' by your expected exception */ typeof(Exception),
                }).SetName(@"set your test name here - set your test description here");
        }
#endregion
 
#region Types
        public struct ExpectedValueTestData<TExpected>
        {
            public Parameters Params;
            public TExpected ExpectedValue;
        }
 
        public struct TestThrowingExceptionData
        {
            public Parameters Params;
            public Type ExpectedException;
        }
 
        public struct Parameters
        {
            public string whom;
        }
#endregion
    }
}

The full source code of this example is available on Github.

Was this article helpful?
Dislike 0
Views: 10

Continue reading

Next: License

We use cookies to give you the best online experience. By agreeing you accept the use of cookies in accordance with our cookie policy.

Privacy Settings saved!
Privacy Settings

When you visit any web site, it may store or retrieve information on your browser, mostly in the form of cookies. Control your personal Cookie Services here.

GetResponse, Google Analytics

We use Google Analytics for marketing purposes. You can disable these cookies.
  • __utmz
  • __utma
  • _ga
  • _gat

We use GetResponse for marketing purposes. This service cannot be disabled, otherwise the website functions will be limited.

Decline all Services
Accept all Services
Get Free Access Now to
9 eBooks!
All about Automated Software Testing
Proven experts
Learn to save up to 75% of your test efforts
Get Free Access Now!
Get Access Now! & Save 50%
Personal Trainer FREE Nutrition Custom Workout App
Get Access Now!
eBook Download
Enter your details to get your free ebook!
All about Automated Software Testing
Download Free Ebook
SUBSCRIBE
MY WEB
NEWSLETTERS
Lorem ipsum dolor sit amet, consectetur adipiscing