MAXIME LEMAITRE – 10/09/2015
xUnit.net
… Assert.Awesome(‘‘xUnit.net’’)…
Agenda
• Business Card
• Why xUnit.net ?
• Compatibility & Comparisons
• Concepts
• Extensibility
• Demo
• Conclusion
• Question
xUnit.net Business Card
• Created in 2007 by 2 ex-Microsofteees
– James Newkirk @jamesnewkirk
– Brad Wilson @bradwilson
• Quick links
– http://xunit.github.io
Official web site
– https://github.com/xunit
850 commits, 800 stars, 35 contributors, 250 forks
– https://twitter.com/xunit
1400 followers
– https://www.nuget.org/packages/xunit
More than 1M downloads (just for the main pkg)
(2007) – “Since the release of NUnit 2.0,
there have been millions of lines of code
written using the various unit testing
frameworks for .NET. About a year ago it
became clear to myself –James- and Brad
that there were some very clear patterns
of success (and failure) with the tools we
were using for writing tests. Rather than
repeating guidance about “do X” or “don’t
do Y”, it seemed like it was the right time to
reconsider the framework itself and see if
we could codify some of those rules.”,
James Newkirk
Lessons learned in Unit Testing (2007)
1. Write tests using the 3A pattern (Arrange, Act, Assert)
2. Keep Your Tests Close (to production code)
3. Use Alternatives to ExpectedException (leads to
uncertainty, violates AAA)
4. Use Small Fixtures (smaller & more focused test classes)
5. Don’t use SetUp/TearDown, TestInit/TestCleanup, …
(improve readability & isolation)
6. Don’t use abstract base test classes (improve readability
& isolation)
7. Improve testability with Inversion of Control (Better test
isolation & decoupled class implementation
Why Build xUnit.net ?
http://bradwilson.typepad.com/presentations/xunit-v2.pdf
• Flexibility: static and private methods
• Reduce Friction: fewer attributes
• Safety: create a new instance for every test
• Be explicit: no control flow in attributes
• Runners: be everywhere the developer is
• Consistency: Prefer the language & framework
• Extensibility: not an afterthought
• TDD first: built with and for TDD
Test Runner Compatibility
Comparing xUnit.net to other frameworks
Unit 2.2 MSTest 2005 xUnit.net 2.x Comments
[Test] [TestMethod] [Fact] Marks a test method.
[TestFixture] [TestClass] n/a
xUnit.net does not require an attribute for a test class; it
looks for all test methods in all public lasses in the assembly.
[ExpectedException]
[ExpectedExce
ption]
Assert.Throws
Record.Exception
xUnit.net has done away with the ExpectedException
[SetUp] [TestInitialize] Constructor
We believe that use of [SetUp] is generally bad. However,
you can implement a parameterless constructor as a direct
replacement.
[TearDown] [TestCleanup] IDisposable.Dispose
We believe that use of [TearDown] is generally bad, but you
can implementIDisposable.Dispose as a direct replacement.
[TestFixtureSetUp] [ClassInitialize] IClassFixture<T> To get per-class fixture setup, use IClassFixture<T>
[TestFixtureTearDown] [ClassCleanup] IClassFixture<T> To get per-class fixture teardown, use IClassFixture<T>
n/a n/a ICollectionFixture<T>
To get per-collection fixture setup and teardown,
implement ICollectionFixture<T> on your test collection.
[Ignore] [Ignore] [Fact(Skip="reason")] Set the Skip parameter on the [Fact] attribute.
[Property] [TestProperty] [Trait] Set arbitrary metadata on a test
n/a [DataSource]
[Theory]
[XxxData]
Theory (data-driven test).
Comparing xUnit.net to other frameworks
NUnit 2.2 MSTest 2005 xUnit.net 1.x Comments
AreEqual
AreNotEqual
AreEqual
AreNotEqual
Equal
NotEqual
MSTest and xUnit.net support generic versions of
this method
AreNotSame
AreSame
AreNotSame
AreSame
NotSame
Same
n/a n/a DoesNotThrow
Ensures that the code does not throw any
exceptions
Greater / Less n/a n/a
xUnit.net alternative: Assert.True(x > y)
Assert.True(x < y)
Ignore Inconclusive n/a
IsEmpty
IsNotEmpty
n/a
Empty
NotEmpty
IsFalse
IsTrue
IsFalse
IsTrue
False
True
IsInstanceOfType
IsNotInstanceOfType
IsInstanceOfType
IsNotInstanceOfType
IsType
IsNotType
IsNotNull
IsNull
IsNotNull
IsNull
NotNull
Null
n/a n/a NotInRange Ensures that a value is not in a given inclusive range
n/a n/a Throws Ensures that the code throws an exact exception
Concepts
Two different major types of unit tests
Facts are tests which are always true.
They test invariant conditions.
Theories are tests which are only true for a
particular set of data (Data Driven Tests)
More data sources for theories
PropertyData ClassData
11
Shared Context between Tests
http://xunit.github.io/docs/shared-context.html
Constructor and Dispose
shared setup/cleanup code
without sharing object instances
Class Fixtures
shared object instance across
tests in a single class
Collection Fixtures
shared object instances across
multiple test classes
Extensibility
xUnit Extensibility
Half a decade of developer requests
• Assert (Sample: AssertExtensions )
Use 3rd party assertions or create custom
• Before/After (Sample : UseCulture)
Run code before & after each test runs
• Class Fixtures (Sample : ClassFixtureExample)
Run code before & after all tests in test class
• Collection Fixtures (Sample: CollectionFixtureExample )
Run code before & after all tests in test collection
• Theory Data (Sample: ExcelDataExample )
Provide new DataAttribute
• Test Ordering (Sample: TestOrderExamples)
• Traits (Sample: TraitExtensibility)
• FactAttribute (Sample: TheoryAttribute)
What does it mean to be a test?
• Test frameworks
What does it mean to find and run tests?
• Runners
And the Test result is GREEN.
The AutoDataAttribute simply uses a Fixture
object to create the objects declared in the
unit tests parameter list (primitives and
complex types like Mock<T>)…
15
Mixing all together …
xUnit+Moq+AutoFixture
What is the result
of this test ?
Demo
Getting Started
http://xunit.github.io/docs/getting-started.html
1. Create a class library
A test project is just a class library
2. Add a reference to xUnit.net
3. Write your first tests
4. Add a reference to a xUnit.net runner
Console or VS
Running Tests
Via Visual Studio Test Explorer
Install-Package
xunit.runner.visualstudio
Via the console test runner
Install-Package xunit.runner.console
Via any test runner …
• Nuget ‘All the way’
No vsix to install, no templates to install, no setups, … simply nuget as we love it
• Great Community & Active Development
xUnit.net is free and open source. The code is hosted on github (850 commits, 3R
contibutors, 800 stars, 250 forks) and the official twitter account has 1400
followers. Many extensions are available (Moq, AutoFixture, …)
• Part of the Next ‘Big Thing’
Do you know ASP.NET 5, Xamarin, DNX, …? xUnit will be the first class citizen and
default choice in .NET in the future
• Well Integrated in the .NET Ecosystem
No troubles to use it because it is already supported everywhere : Team
Foundation Server, CruiseControl.net, AppVeyor, TeamCity, Resharper …
• It Helps to Write “Better, Faster, Stronger” Tests
This is the essence of xUnit.net. codify patterns of success (and failure)
19
Why moving to xUnit ?
Questions
References
• http://xunit.github.io/
• http://jamesnewkirk.typepad.com/LessonsLearnedinProgrammerTesting.pdf
• http://bradwilson.typepad.com/presentations/xunit-v2.pdf
• http://www.codeproject.com/Articles/825248/The-Dynamic-Duo-of-Unit-Testing-xUnit-net-and-Auto
• https://github.com/xunit/samples.xunit
• http://blog.ploeh.dk/2010/10/08/AutoDataTheorieswithAutoFixture/
About Us
• Betclic Everest Group, one of the world leaders in online
gaming, has a unique portfolio comprising various
complementary international brands: Betclic, Everest
Poker/Casino, Bet-at-home, Expekt, Imperial Casino, Monte-
Carlo Casino…
• Through our brands, Betclic Everest Group places expertise,
technological know-how and security at the heart of our
strategy to deliver an on-line gaming offer attuned to the
passion of our players. We want our brands to be easy to use
for every gamer around the world. We’re building our
company to make that happen.
• Active in 100 countries with more than 12 million customers
worldwide, the Group is committed to promoting secure and
responsible gaming and is a member of several international
professional associations including the EGBA (European
Gaming and Betting Association) and the ESSA (European
Sports Security Association).
We want our Sports betting, Poker, Horse racing and
Casino & Games brands to be easy to use for every
gamer around the world. Code with us to make that
happen.
Look at all the challenges we offer HERE
Check our Employer Page
Follow us on LinkedIn
WE’RE HIRING !

Mini training - Moving to xUnit.net

  • 1.
    MAXIME LEMAITRE –10/09/2015 xUnit.net … Assert.Awesome(‘‘xUnit.net’’)…
  • 2.
    Agenda • Business Card •Why xUnit.net ? • Compatibility & Comparisons • Concepts • Extensibility • Demo • Conclusion • Question
  • 3.
    xUnit.net Business Card •Created in 2007 by 2 ex-Microsofteees – James Newkirk @jamesnewkirk – Brad Wilson @bradwilson • Quick links – http://xunit.github.io Official web site – https://github.com/xunit 850 commits, 800 stars, 35 contributors, 250 forks – https://twitter.com/xunit 1400 followers – https://www.nuget.org/packages/xunit More than 1M downloads (just for the main pkg) (2007) – “Since the release of NUnit 2.0, there have been millions of lines of code written using the various unit testing frameworks for .NET. About a year ago it became clear to myself –James- and Brad that there were some very clear patterns of success (and failure) with the tools we were using for writing tests. Rather than repeating guidance about “do X” or “don’t do Y”, it seemed like it was the right time to reconsider the framework itself and see if we could codify some of those rules.”, James Newkirk
  • 4.
    Lessons learned inUnit Testing (2007) 1. Write tests using the 3A pattern (Arrange, Act, Assert) 2. Keep Your Tests Close (to production code) 3. Use Alternatives to ExpectedException (leads to uncertainty, violates AAA) 4. Use Small Fixtures (smaller & more focused test classes) 5. Don’t use SetUp/TearDown, TestInit/TestCleanup, … (improve readability & isolation) 6. Don’t use abstract base test classes (improve readability & isolation) 7. Improve testability with Inversion of Control (Better test isolation & decoupled class implementation
  • 5.
    Why Build xUnit.net? http://bradwilson.typepad.com/presentations/xunit-v2.pdf • Flexibility: static and private methods • Reduce Friction: fewer attributes • Safety: create a new instance for every test • Be explicit: no control flow in attributes • Runners: be everywhere the developer is • Consistency: Prefer the language & framework • Extensibility: not an afterthought • TDD first: built with and for TDD
  • 6.
  • 7.
    Comparing xUnit.net toother frameworks Unit 2.2 MSTest 2005 xUnit.net 2.x Comments [Test] [TestMethod] [Fact] Marks a test method. [TestFixture] [TestClass] n/a xUnit.net does not require an attribute for a test class; it looks for all test methods in all public lasses in the assembly. [ExpectedException] [ExpectedExce ption] Assert.Throws Record.Exception xUnit.net has done away with the ExpectedException [SetUp] [TestInitialize] Constructor We believe that use of [SetUp] is generally bad. However, you can implement a parameterless constructor as a direct replacement. [TearDown] [TestCleanup] IDisposable.Dispose We believe that use of [TearDown] is generally bad, but you can implementIDisposable.Dispose as a direct replacement. [TestFixtureSetUp] [ClassInitialize] IClassFixture<T> To get per-class fixture setup, use IClassFixture<T> [TestFixtureTearDown] [ClassCleanup] IClassFixture<T> To get per-class fixture teardown, use IClassFixture<T> n/a n/a ICollectionFixture<T> To get per-collection fixture setup and teardown, implement ICollectionFixture<T> on your test collection. [Ignore] [Ignore] [Fact(Skip="reason")] Set the Skip parameter on the [Fact] attribute. [Property] [TestProperty] [Trait] Set arbitrary metadata on a test n/a [DataSource] [Theory] [XxxData] Theory (data-driven test).
  • 8.
    Comparing xUnit.net toother frameworks NUnit 2.2 MSTest 2005 xUnit.net 1.x Comments AreEqual AreNotEqual AreEqual AreNotEqual Equal NotEqual MSTest and xUnit.net support generic versions of this method AreNotSame AreSame AreNotSame AreSame NotSame Same n/a n/a DoesNotThrow Ensures that the code does not throw any exceptions Greater / Less n/a n/a xUnit.net alternative: Assert.True(x > y) Assert.True(x < y) Ignore Inconclusive n/a IsEmpty IsNotEmpty n/a Empty NotEmpty IsFalse IsTrue IsFalse IsTrue False True IsInstanceOfType IsNotInstanceOfType IsInstanceOfType IsNotInstanceOfType IsType IsNotType IsNotNull IsNull IsNotNull IsNull NotNull Null n/a n/a NotInRange Ensures that a value is not in a given inclusive range n/a n/a Throws Ensures that the code throws an exact exception
  • 9.
  • 10.
    Two different majortypes of unit tests Facts are tests which are always true. They test invariant conditions. Theories are tests which are only true for a particular set of data (Data Driven Tests)
  • 11.
    More data sourcesfor theories PropertyData ClassData 11
  • 12.
    Shared Context betweenTests http://xunit.github.io/docs/shared-context.html Constructor and Dispose shared setup/cleanup code without sharing object instances Class Fixtures shared object instance across tests in a single class Collection Fixtures shared object instances across multiple test classes
  • 13.
  • 14.
    xUnit Extensibility Half adecade of developer requests • Assert (Sample: AssertExtensions ) Use 3rd party assertions or create custom • Before/After (Sample : UseCulture) Run code before & after each test runs • Class Fixtures (Sample : ClassFixtureExample) Run code before & after all tests in test class • Collection Fixtures (Sample: CollectionFixtureExample ) Run code before & after all tests in test collection • Theory Data (Sample: ExcelDataExample ) Provide new DataAttribute • Test Ordering (Sample: TestOrderExamples) • Traits (Sample: TraitExtensibility) • FactAttribute (Sample: TheoryAttribute) What does it mean to be a test? • Test frameworks What does it mean to find and run tests? • Runners
  • 15.
    And the Testresult is GREEN. The AutoDataAttribute simply uses a Fixture object to create the objects declared in the unit tests parameter list (primitives and complex types like Mock<T>)… 15 Mixing all together … xUnit+Moq+AutoFixture What is the result of this test ?
  • 16.
  • 17.
    Getting Started http://xunit.github.io/docs/getting-started.html 1. Createa class library A test project is just a class library 2. Add a reference to xUnit.net 3. Write your first tests 4. Add a reference to a xUnit.net runner Console or VS
  • 18.
    Running Tests Via VisualStudio Test Explorer Install-Package xunit.runner.visualstudio Via the console test runner Install-Package xunit.runner.console Via any test runner …
  • 19.
    • Nuget ‘Allthe way’ No vsix to install, no templates to install, no setups, … simply nuget as we love it • Great Community & Active Development xUnit.net is free and open source. The code is hosted on github (850 commits, 3R contibutors, 800 stars, 250 forks) and the official twitter account has 1400 followers. Many extensions are available (Moq, AutoFixture, …) • Part of the Next ‘Big Thing’ Do you know ASP.NET 5, Xamarin, DNX, …? xUnit will be the first class citizen and default choice in .NET in the future • Well Integrated in the .NET Ecosystem No troubles to use it because it is already supported everywhere : Team Foundation Server, CruiseControl.net, AppVeyor, TeamCity, Resharper … • It Helps to Write “Better, Faster, Stronger” Tests This is the essence of xUnit.net. codify patterns of success (and failure) 19 Why moving to xUnit ?
  • 20.
  • 21.
    References • http://xunit.github.io/ • http://jamesnewkirk.typepad.com/LessonsLearnedinProgrammerTesting.pdf •http://bradwilson.typepad.com/presentations/xunit-v2.pdf • http://www.codeproject.com/Articles/825248/The-Dynamic-Duo-of-Unit-Testing-xUnit-net-and-Auto • https://github.com/xunit/samples.xunit • http://blog.ploeh.dk/2010/10/08/AutoDataTheorieswithAutoFixture/
  • 22.
    About Us • BetclicEverest Group, one of the world leaders in online gaming, has a unique portfolio comprising various complementary international brands: Betclic, Everest Poker/Casino, Bet-at-home, Expekt, Imperial Casino, Monte- Carlo Casino… • Through our brands, Betclic Everest Group places expertise, technological know-how and security at the heart of our strategy to deliver an on-line gaming offer attuned to the passion of our players. We want our brands to be easy to use for every gamer around the world. We’re building our company to make that happen. • Active in 100 countries with more than 12 million customers worldwide, the Group is committed to promoting secure and responsible gaming and is a member of several international professional associations including the EGBA (European Gaming and Betting Association) and the ESSA (European Sports Security Association).
  • 23.
    We want ourSports betting, Poker, Horse racing and Casino & Games brands to be easy to use for every gamer around the world. Code with us to make that happen. Look at all the challenges we offer HERE Check our Employer Page Follow us on LinkedIn WE’RE HIRING !