Today was the first day of my .NET 4.0 training. And I would like to share the highlights of each day. Well at least the highlights for me. So this is the first day of 5.
Introductions and first days are always slow. So this was no exception. There were some basic introduction things for people who have newer came into contact with electricity. But a good 5 hours in something was presented that caught my eye: Pex and Moles.
Pex and Moles are actually two separate peaces of software:
- Pex automatically generates test suites with high code coverage.
- Moles allows to replace any .NET method with a delegate.
I am still unsure about the real day-to-day value that moles will offer me. This is not because the software is not up to pair with what I would use but that I am mostly working on code I can change and refactor so that the need for such a tool is not needed. But more on that later on.
First you will need a copy of Pex and Moles. So here is the download link. You can even find a version for the express version (non-commercial). After you have the file just let the installer do its work an tolerate the 2 ~ 3 times your focus will be stolen (it is worth it).
First you need some code to let pex have fun with. I just quickly wrote a little class with one method. And here it is:
namespace PexAndMoles { public class Calculator { public int Add(int one, int two) { if(one == 0 || two == 0) throw new ArgumentException(); if(one < two) throw new ArgumentException(); return one + two; } } }
There is a reason for all those ifs in there. It is for the sole reason to give pex something to work on 🙂
So to get started just left-click on the method you want to “work on”. You should see something like this.
Pex will ask you which testing framework it should use. You can choose from all the major testing framework. But to keep it simple I chose to stick with MSUnit.
After a short time where you are tempted by a “follow us on Facebook” link the results are presented and if you are lucky (depending on the code complexity) pex will find all major test scenarios for your method.
In my case this is what it came up with.
And those are all the test scenarios I wanted (or even expected).
Now that you have your tests you want to keep them for later (most probably some sort of regression testing). So pex can help you there to. If you select all created “results” a “Promote…” button will appear. If pressed it adds the “results” as unit tests into your testing project or creates a new one and adds them there.
The code generated is confusing at worst and funny at best. It is not the go-to example of good/clean code. But it is auto-generated and can be regenerated if future changes break the tests. The naming convention is “acceptable”. Before I rant too much here is the code generated:
namespace PexAndMoles { [TestClass] [PexClass(typeof(Calculator))] [PexAllowedExceptionFromTypeUnderTest(typeof(ArgumentException), AcceptExceptionSubtypes = true)] [PexAllowedExceptionFromTypeUnderTest(typeof(InvalidOperationException))] public partial class CalculatorTest { [PexMethod] public int Add( [PexAssumeUnderTest]Calculator target, int one, int two ) { int result = target.Add(one, two); return result; // TODO: add assertions to method CalculatorTest.Add(Calculator, Int32, Int32) } [TestMethod] [ExpectedException(typeof(ArgumentException))] public void AddThrowsArgumentException547() { int i; Calculator s0 = new Calculator(); i = this.Add(s0, 0, 0); } [TestMethod] [ExpectedException(typeof(ArgumentException))] public void AddThrowsArgumentException81() { int i; Calculator s0 = new Calculator(); i = this.Add(s0, 1, 0); } [TestMethod] public void Add520() { int i; Calculator s0 = new Calculator(); i = this.Add(s0, 1, 1); Assert.AreEqual<int>(2, i); Assert.IsNotNull((object)s0); } [TestMethod] [ExpectedException(typeof(ArgumentException))] public void AddThrowsArgumentException470() { int i; Calculator s0 = new Calculator(); i = this.Add(s0, 2, 3); } } }
And that is pex in a nutshell. At least that is what I was able to find out about it in the half day I spend with it.
I know that Moles was not mentioned here but I would like to spend some more time with it before writing about it in more detail, besides the post is long enough.
And that is all the time I have today.
Thx for your time.