View
591
Download
0
Embed Size (px)
Citation preview
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/xunit1400 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 frameworksUnit 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] [ExpectedException]
Assert.ThrowsRecord.Exception xUnit.net has done away with the ExpectedException
[SetUp] [TestInitialize] ConstructorWe 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 frameworksNUnit 2.2 MSTest 2005 xUnit.net 1.x CommentsAreEqualAreNotEqual
AreEqualAreNotEqual
EqualNotEqual
MSTest and xUnit.net support generic versions of this method
AreNotSameAreSame
AreNotSameAreSame
NotSameSame
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 IsEmptyIsNotEmpty n/a Empty
NotEmpty
IsFalseIsTrue
IsFalseIsTrue
FalseTrue
IsInstanceOfTypeIsNotInstanceOfType
IsInstanceOfTypeIsNotInstanceOfType
IsTypeIsNotType
IsNotNullIsNull
IsNotNullIsNull
NotNullNull
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 testsFacts 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 sources for theories
PropertyData ClassData
Shared Context between Testshttp://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 Fixturesshared object instances across
multiple test classes
Extensibility
xUnit ExtensibilityHalf 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
15
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>)…
Mixing all together …xUnit+Moq+AutoFixture
What is the result of this test ?
Demo
Getting Startedhttp://xunit.github.io/docs/getting-started.html
1. Create a class libraryA 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 runnerConsole or VS
Running Tests
Via Visual Studio Test ExplorerInstall-Package xunit.runner.visualstudio
Via the console test runnerInstall-Package xunit.runner.console
Via any test runner …
19
• Nuget ‘All the way’No vsix to install, no templates to install, no setups, … simply nuget as we love it
• Great Community & Active DevelopmentxUnit.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 EcosystemNo 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” TestsThis is the essence of xUnit.net. codify patterns of success (and failure)
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 !