That Conference 2016, Kalahari Resort, Lake Delton, WI
Unit Testing with xUnit.net – Chris Gardner
Day 1, 8 Aug 2016
Disclaimer: This post contains my own thoughts and notes based on attending That Conference 2016 presentations. Some content maps directly to what was originally presented. Other content is paraphrased or represents my own thoughts and opinions and should not be construed as reflecting the opinion of the speakers.
Executive Summary
- xUnit.net almost always a better choice than MsTest or nUnit
Why a new framework?
- MSTest – gets the job done, but not always pretty
- nUnit – came from jUnit
- xUnit – two guys from nUnit started
Single instance per fact
- MSTest runs each method in a class
- Xunit.net creates individual container per fact (test)
- Different instances mean that they are completely independent
- Same instance, order of tests matter, because instance has state
Everything in one place
- MSTest has 3 different places for initialization (class, assembly, etc)
- xUnit does this in one place
- AAA
- Arrange – set up test (only what I need)
- Act – single operation
- Assert – make assertions (preferably only one assertion)
- When assertion fails, it skips others
- So better to have just one assertion
No ExpectedExceptionAttribute
- Shouldn’t ever do this
- Why bad
- You’re saying entire method could throw exception
- You didn’t isolate where the exception could come from
- Instead
- try/block, assert/fail right at that point
Reduce custom attributes
xUnit reduces the number of custom attributes that you need to use
Use language features
- xUnit takes advantage of C# features (e.g. lambdas)
Extensibility
- Completely open source
- Can add in tweaks without rebuilding engine
- e.g. custom validation
What MSTest did right
- Faster Discovery
- When you re-compile, discovery has to happen
- Test impact analysis
- When you make code change, only run tests that would be affected by recent code change
xunit.github.io
Performance
- xUnit runs separate classes in parallel
- And different methods in one class serially
- Can be more performant than MsTest
Comparisions