Automated method for converting ExpectedException in Nunit

Posted by: tfabris

Automated method for converting ExpectedException in Nunit - 08/07/2016 21:03

Anyone have any ideas about this one? My google searches are turning up nothing at all.

I'm tasked with performing two changes to our company's code base:
- Convert all tests which used to use MSTest/VStest to NUnit.
- Convert all tests which used to use NUnit 2.x to NUnit 3.x.

Converting VSTest->Nunit is mostly just search and replace, with maybe a little hand tweaking.

NUnit 2.x->3.x is quite tricky because of the fuckton of breaking changes involved that I have to work around, which aren't just plain old search-and-replace operations. So far, the trickiest change I see is that Nunit 3.x has removed the [ExpectedException] attribute in favor of testing Assert.Throws( ()-> ...

Does anyone have a link to any resources which might have scripts or programs to help with the conversion? Here's an example of the kind of conversion I need to do:

Code:

  ORIGINAL:

        [Test]
        [ExpectedException(typeof(MyCompany.Diagnostics.AssertionFailedException), ExpectedMessage = "action should not be null.", MatchType = MessageMatch.Contains)]
        public void ExecuteWithNullAction()
        {
            new CancellationManager(this.Token).Execute(null);
        }
		
  REPLACEMENT:

        [Test]
        public void ExecuteWithNullAction()
        {
            Assert.That(() => new CancellationManager(this.Token).Execute(null),
                Throws.Exception
                .TypeOf<MyCompany.Diagnostics.AssertionFailedException>()
                .With.Property("Message")
                .StringContaining("action should not be null.")
                );
        }


And it's not even always that simple. Maybe the call to the code under test is buried among dozens of lines. Since there are hundreds of such instances in our code base already, it's a bit too daunting to do each one by hand.

Any ideas what would make this easier?
Posted by: tfabris

Re: Automated method for converting ExpectedException in Nunit - 08/07/2016 22:19

Currently investigating to see if Resharper's Structural Search and Replace feature could help with this.
Posted by: tfabris

Re: Automated method for converting ExpectedException in Nunit - 06/09/2016 17:34

Following up:

I couldn't get the R# structural search and replace working properly. Though it *should* have worked, it's pretty buggy and hard to use in its implementation. I couldn't get it working correctly so I abandoned it.

Instead I wrote a piece of Roslyn code which did the work for me. Roslyn has its own set of problems, but I was able to get an implementation working successfully. I was also able to use Roslyn to do a couple of other Nunit2->Nunit3 conversions which were needed.

The Roslyn app that I wrote is internal company-owned code, so I'm not able to post it at the moment. Maybe someday I'll get permission to put it up on GitHub or something.