Some more on Pex
So I've been playing with Pex and I decided to try it on another application I am playing with Prism.
I had a look at the ListDictionaryFixture in the Prism.Tests project. And I set my eyes on these three TestMethods and decided to see how Pex could work here.
1: [ExpectedException(typeof(ArgumentNullException))]
2: [TestMethod]
3: public void AddThrowsIfKeyNull()
4: {
5: list.Add(null, new object());
6: }
7:
8: [ExpectedException(typeof(ArgumentNullException))]
9: [TestMethod]
10: public void AddThrowsIfValueNull()
11: {
12: list.Add("", null);
13: }
14:
15: [TestMethod]
16: public void CanAddValue()
17: {
18: object value1 = new object();
19: object value2 = new object();
20:
21: list.Add("foo", value1);
22: list.Add("foo", value2);
23:
24: Assert.AreEqual(2, list["foo"].Count);
25: Assert.AreSame(value1, list["foo"][0]);
26: Assert.AreSame(value2, list["foo"][1]);
27: }
So first I referenced the Microsoft.Pex.Framework Assembly. Then I created a parameterized PexMethod.
1: [PexMethod]
2: public void TestCanAddValue(string key, object value)
3: {
4: list.Add(key, value);
5: }
And then I ran Pex.
My first result was not quite what I expected:
I would have expected at least three tests to be run by Pex, so first I check the Uninstrumented Method.
If you right-click and select Instrument assembly Pex goes and adds a PexAssemblyInfo.cs file to your project
and places the following inside the file:
1: using Microsoft.Pex.Framework.Instrumentation;
2: using System;
3: using Microsoft.Pex.Framework.Validation;
4:
5: [assembly: PexInstrumentAssembly("Prism")]
Right, so let's run Pex again. After Pex has run it shows me the following result:
This is what I expected. Now I want to fix the failed Run 1 and 2. If you look at the generated test you see that it does not say what the ExpectedException should be:
1: [TestMethod]
2: [PexRaisedException(typeof(ArgumentNullException))]
3: [PexGeneratedBy(typeof(ListDictionaryFixture))]
4: public void TestCanAddValueStringObject_20080604_155328_000()
5: {
6: this.TestCanAddValue((string)null, (object)null);
7: }
8:
9: [TestMethod]
10: [PexRaisedException(typeof(ArgumentNullException))]
11: [PexGeneratedBy(typeof(ListDictionaryFixture))]
12: public void TestCanAddValueStringObject_20080604_155926_001()
13: {
14: this.TestCanAddValue("", (object)null);
15: }
To get Pex to generate the tests correctly you need to either Fix It or Allow It by right clicking on the row:
I chose to Allow It and then it adds another entry into the PexAssemblyInfo.cs file.
And then I reran Pex, and there you go I replaced three Unit Tests with one PexMethod.
Comments