Skip to content
This repository has been archived by the owner on Nov 10, 2019. It is now read-only.

Latest commit

 

History

History
151 lines (103 loc) · 5.97 KB

exercise03.md

File metadata and controls

151 lines (103 loc) · 5.97 KB

Exercise 3

Learnings

  1. Basics about test projects
  2. Mocking dependencies using Fakes
  3. Hosting Web API in tests using OWIN
  4. Creating an automated Web Test

Add Test Project

  1. Discussion points:

    • Describe why test automation is very important for short iteration times and continuous delivery
    • Discuss different types of automated tests (e.g. unit tests, integration tests, automated UI tests, etc.)
    • Discuss the importance of fast tests (e.g. less costs for hosted build controllers, fast tests are executed more often, less waiting time for dev teams, etc.) and how mocking of backend services can help to achieve that
  2. Add a test project to your Visual Studio solution.
    Add test project

  3. Remove generated UnitTest1.cs.

  4. Switch to latest version of .NET (4.6.1) in the Books.Test project properties page just like you did for the Books project in exercise 1

  5. Add a reference to the Web API project from your new test project.
    Add reference

  6. Add references to the following framework assemblies:

    • System.ComponentModel.Composition
    • System.ComponentModel.Composition.Registration
    • System.Reflection.Context
  7. Install necessary NuGet packages by running the following commands in Visual Studio's Package Manager Console (you can use Manage NuGet Packages for Solution instead if you prefer GUI over PowerShell):

    • Install-Package Microsoft.AspNet.WebApi.Owin -Project Books.Test
    • Install-Package Microsoft.Owin.Host.SystemWeb -Project Books.Test
    • Install-Package Microsoft.AspNet.WebApi.OwinSelfHost -Project Books.Test

Add and Run Tests

  1. Discussion points:

    • Describe the concept of mocking
    • Short introduction to Microsoft Fakes (shims vs. stubs)
  2. Add Fakes assembly for Books reference:
    Add Fakes

  3. Replace Fakes/Books.fakes with the following code:

     <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
         <Assembly Name="Books"/>
         <StubGeneration>
             <Clear/>
             <Add TypeName="INameGenerator"/>
         </StubGeneration>
         <ShimGeneration>
             <Clear/>
         </ShimGeneration>
     </Fakes>
    
  4. Copy .cs files from Assets/Exercise-3-Tests into your test project. Make yourself familiar with the two test files.

  5. Discussion points:

    • Short introduction into unit testing with Visual Studio
    • Describe how OWIN is used to host a web server in an integration test (IntegrationTest.cs)
  6. Build your test project. There should not be errors.

  7. Open Visual Studio's Test Explorer and run all tests.
    Test Explorer

Add Web Test

  1. Discussion points:

  2. Add a new Web Performance and Load Test Project.
    Add Web Test Project

  3. Rename test to GetBooksTest
    Rename test

  4. Add web request to our Get Books Web API.
    Add request to Web Test

  5. Discussion points:

    • Describe other capabilities of Visual Studio Web Tests (e.g. loops, conditions, data-driven tests, parameters)
  6. Open the request's properties and change the URL appropriately.
    Change URL in request's properties

  7. Discussion points:

    • Describe other request properties offered by Visual Studio
  8. Parameterize web server. This is important if you have lots of requests. With parameters, tests become much easier to maintain.
    Parameterize Web Server

  9. Make yourself familiar with the web test after server parameterization.
    Test after parameterization

  10. Discussion points:

    • Describe other request properties offered by Visual Studio
    • Describe validation rules
  11. Start an instance of your OWIN web server (press Ctrl+F5 to start it without debugger).

  12. Run the web test.
    Run web test

  13. Verify that test was successful.
    Verify test results

Add Load Test

  1. Discussion points:

    • Discuss the importance of load testing
    • General overview over Visual Studio load testing capabilities
  2. Add load test to web test project.
    Add load test

  3. Select the following test settings (select default values for settings not mentioned here):

    • Select On-premise Load Test (we will move to the cloud later)
    • 100 test iterations
    • 5 seconds sampling rate
    • Constant load of 10 users
    • Add the GetBooksTest to the load test
  4. Discussion points:

    • Describe other capabilities of Visual Studio load testing
  5. Run load test.
    Run load test

  6. Analyze load test results.
    Analyze results

Further Ideas

If you have time left, you could additionally cover topics like:

  • Create custom validation rule
  • Use web UI from final sample code to demo HTML-related web test features
  • (Not recommended but possible if you have a very web-development-oriented audience) Add a Jasmine unit test for Angular client