This is the first post about Pex and how to use it. Since Pex is a fairly large project, I'll probably stretch the content of a large number of entries. Stay tunned...
Pex gives you Parameterized Unit Tests
Data-driven tests are not something new. They exist under different forms such as MbUnit's RowTest or CombinatorialTest, VSTS's DataSource, etc... So what's so special about Pex? The major difference is that Pex finds the parameter inputs for you (and generates a unit test out of it).
Whereas the user had to (smartly) guess a set of input for the data-driven tests, Pex tries to compute the relevant inputs to those tests automatically (and may also suggest fixes).
Note: The way Pex finds the input will be covered in more details later. In a couple words, Pex performs a systematic white box analysis of the program behavior. It tries to generate a minimal test suite with maximum coverage.
Parameterized Unit Tests, what does it feel like?
Parameterized unit tests are methods with parameters. There's nothing magic about that. Pex provides a set of custom attributes so that you can author them side-by-side with classic unit tests:
[TestClass, PexClass] // VSTS fixture containing parameterized testspublic class AccountTest{ // parameterized unit test // account money should never be negative, for any 'money', 'withdraw' [PexTest] public void TransferFunds(int money, int withdraw) {...}}
When Pex finds interresting data to feed the parameterized unit test, it generates a unit test method that calls the parameterized unit test. This also means that once the unit test has been generated, you do not need Pex anymore to run the repro.
[TestClass, PexClass] // VSTS fixture containing parameterized testspublic class AccountTest{ [PexTest] public void TransferFunds(int money, int withdraw) {...} ... [TestMethod, GeneratedBy("Pex", "1.0.0.0")] public void TransferFunds_12345() { this.TransferFunds(12, 13); }}
Why do I need parameterized unit tests anyway?
A parameterized unit tests generally captures more program behaviors than a single unit test which is like a micro-scenario. This will become more apparent when we start looking at some examples.
If you were already using [RowTest] or [DataSource] as part of your testing, then you will definitely like Pex.
Page rendered at Thursday, December 04, 2008 7:38:57 AM UTC
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.