I know I should be Unit Testing, but I don't know how or where to start - workshop version

February 14, 2019    Development UnitTesting DevOps BDD AutomatedTesting

I know I should be Unit Testing, but I don’t know how or where to start - Basic Workshop Version

I started writing about and giving presentations last year. This year, our company has decided to use that material and give a “Getting started with Unit Testing workshop”. I’ve given this 3 times now at Omnitech. Once to a pilot group, then to our normal Tuesday Lunch and Learn group, then to those who didn’t make it. I’ve been improving from feedback each time. I’m thankful for leadership that have seen the importance of automated testing to the quality of our code and to our organization. I was alloted some time to work on this.

Here’s the format I used.

Goal

To get people writing tests and get over the “but” stage of not testing.

We’ll take a simple, slightly pre-baked tests and write them together with Kevin narrating.

Preparation

Requires VS 2017 (latest), .Net Core 2.2 (or newer as time goes on), node.js (https://nodejs.org/en/, LTS, this is installed with VS if you check the right feature) You’ll need .Net Core 2.2 installed, check your Add or Remove programs or in the command line dotnet –version Download from https://dotnet.microsoft.com/download/dotnet-core/2.2 => get the sdk, if you don’t have *

Format

  • One hour lunch period
  • Leader’s screen up on the TV
  • Follow along
  • Adding tests to the get weather api and api.tests
  • We’ll be using Visual Studio 2017 (or newer as time goes on)

From the PowerPoint linked above

Terms Slide #3

What should we test? Slide #7

Create a test project and Naming

Test Organization

  • [TestCategory]
  • TestCategories static class
  • // Arrange // Act // Assert (makes a good VS snippet)
    • Create a .snippet file and Drop this in C:\Users{username}\Documents\Visual Studio 2019\Code Snippets\Visual C#\My Code Snippets
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>aaa</Title>
            <Shortcut>aaa</Shortcut>
            <Description>AAA for tests</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>

            </Declarations>
            <Code Language="csharp"><![CDATA[
                // Arrange

                // Act

                // Assert
            ]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Test Naming: Class_Action_Modifier_ExpectedResult “ have decent titles that communicate their intent.” Organization in the test explorer is important. I usually look at it by category.

What are we testing?

  • Given When Then helps define and use a common (ubiquitous language)
  • Starting from the WeatherController top level down, you could start the other way
    • Testing the controller may be a bit debatable. There shouldn’t be much code and 0 business rule code in the controller, but there is flow code that may be important.
  • Then looking at the ApixuClient

TDD or not to TDD?

  • It depends and is up to you
  • TDD is a good tool when you don’t really know what you’re doing
  • It helps you create less classes and code and makes sure your tests are correct.

We are doing TDD “light” with removing comments from code walk through following along

First test - WeatherController_GetCurrentTemp_NoZipCode_Returns400

Given an API call When asking for current temp and no zip code is given Then returns a 400

Type it out, don’t just un comment

  • Write the test, Red > Green > Refactor repeat
  • Inconclusive > add to test code to get red (factory) > un comment in WeatherController
  • Talk about why, recap

Pause for questions and for people to catch up.

Next Test - WeatherController_GetCurrentTemp_NoOrBadZipCode_Returns400

  • requirements changed - Replace the first test (delete it!)
  • change first with [DataRow]
  • Talk about DataRow
  • Fix Controller code

ApixuClientTests

ApixuClientTests_GetTemp_GivenAZipCode_ReturnsTemp

Given a request to Apixu to get the temp When given a zip code It Returns the temp

ApixuClientTests_GetTemp_GivenAZipCode_UsesThatZipCode

Given a request to Apixu to get the temp When given a zip code It uses that zip code // not the best, most needed test?

ApixuClientTests_GetTemp_GivenAZipCode_NotFound_ThrowsException

  • Handling error states outside of our control

No validation, that’s the controller job in this application (for better or worse)

Where to Start?

Slide #8

Tools

  • MSTest v2 (via the Nuget Package)
    • Nunit would work too, but we’ll use MSTest
    • Csproj needed true or the tests weren’t being found
  • MOQ or FakeItEasy for helping with Fakes and Moqs
    • You’ll see FakeItEasy, but MOQ seems to be more popular
  • Live Unit Testing in Visual Studio
    • Right click test project to exclude all but this
  • VS Code has a test explorer extension, but that only works for a project, not in a solution view

If you’re close to Sioux Falls, SD or want to do a remote workshop, let me know on Twitter and we can work something out.



comments powered by Disqus

Please consider using Brave and adding me to your payment ledger. Then you won't have to see ads!

Support me and download Brave!

Use Brave