Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C# unit test framework that supports arbitrary expressions rather than a limited set of adhoc methods?

Tags:

Basically NUnit, xUnit, MbUnit, MsTest and the like have methods similar to the following:

Assert.IsGreater(a,b) //or, a little more discoverable Assert.That(a, Is.GreaterThan(b)) 

However, there are a limited number of such comparison operators built-in; and they duplicate the languages operators needlessly. When I want anything even slightly complex, such as...

Assert.That(a.SequenceEquals(b)) 

I'm often either left digging through the manual to find the equivalent of the expression in NUnit-speak, or am forced to fall-back to plain boolean assertions with less helpful error messages.

C#, however, integrates well with arbitrary Expressions - so it should be possible to have a method with the following signature:

void That(Expression<Func<bool>> expr); 

Such a method could be used to both execute the test (i.e. validate the assertion) and to also provide less-opaque diagnostics in case of test failure; after all, an expression can be rendered to pseudo-code to indicate which expression failed; and with some effort, you could even evaluate failing expressions intelligently to give some clue of the value of subexpressions.

For example:

Assert.That(()=> a == b);//could inspect expression and print a and b Assert.That(()=> a < b && b < c); //could mention the values of "a<b" and "b<c" and/or list the values of a, b, and c. 

At a minimum, it would make the use of a parallel language for expressions unnecessary, and in some cases it might make failure messages more useful.

Does such a thing exist?

Edit: After trying (and liking!) Power Assert, I ended up reimplementing it to address several limitations. My variant of this is published as ExpressionToCode; see my answer below for a list of improvements.

like image 871
Eamon Nerbonne Avatar asked Dec 01 '10 15:12

Eamon Nerbonne


People also ask

Why is there a slash in AC?

Senior Member. You read it as "ei-cee" (no "slash" pronounced). In terms of distinguishing between "air conditioning" and "air conditioner," I can think of an example like "Today, I bought a new air conditioner" ("conditioning" not allowed). I personally would not say "Today, I bought a new AC."

What is the mean of AC?

a/ c is an abbreviation for air-conditioning. Keep your windows up and the a/c on high. 60 Motel Units. All Units A/C, Heat, Cable TV.

Why is it called AC?

Air conditioning, often abbreviated as A/C or AC, is the process of removing heat from an enclosed space to achieve a more comfortable interior environment (sometimes referred to as 'comfort cooling') and in some cases also strictly controlling the humidity of internal air.

Is AC the same as AC?

The term “A/C” stands for “air conditioning,” but it's frequently used to describe any type of home cooling equipment, such as a traditional split-system air conditioner or heat pump, mini-split unit, geothermal system, or even a window unit.


1 Answers

Check out the PowerAssert library (example output below):

PAssert.IsTrue(() => x + 5 == d.Month * y);   System.Exception : IsTrue failed, expression was:  x + 5 == d.Month * y | |   |  | |     | | | |   |  | |     | 6 | |   |  | |     18 | |   |  | 3 | |   |  01/03/2010 00:00:00 | |   False | 16 11 

http://powerassert.codeplex.com/

like image 151
Guy Avatar answered Nov 11 '22 08:11

Guy