Assert.areequal output value formatting

788 views
Skip to first unread message

Ss Gg

unread,
May 30, 2016, 4:01:35 PM5/30/16
to NUnit-Discuss

Hi, I would like to ask for a help  with formatting output values from Assert.Areequal  I would like to see the values in hexa for example. Thx

Charlie Poole

unread,
May 30, 2016, 4:05:03 PM5/30/16
to NUnit-Discuss
There are two ways you can control how the values shown in a message
are formatted:

1. For classes you define yourself, override ToString(). The output of
ToString() will appear in the "Expected" and "but was" lines of the
standard equality failure message.
2. Include the formatted output in a custom message that you add as
part of the Assert Statement.

Charlie
> --
> You received this message because you are subscribed to the Google Groups
> "NUnit-Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nunit-discus...@googlegroups.com.
> To post to this group, send email to nunit-...@googlegroups.com.
> Visit this group at https://groups.google.com/group/nunit-discuss.
> For more options, visit https://groups.google.com/d/optout.

Ss Gg

unread,
May 31, 2016, 1:36:18 AM5/31/16
to NUnit-Discuss
Thanks for an answer.

Assert.AreEqual( object expected, object actual, string message, 
                 object[] parms );

Do you know what kind of params can be used, this would be for me the easiest way how to change existing asserts.Just add formatting paremeters into existing
asserts

Thanks



Dňa pondelok, 30. mája 2016 22:05:03 UTC+2 charlie napísal(-a):

Charlie Poole

unread,
May 31, 2016, 11:58:25 AM5/31/16
to NUnit-Discuss
Sure.. just use standard .NET string formatting as you would with
`string.Format` or `Console.WriteLine`

Charlie

On Mon, May 30, 2016 at 10:36 PM, 'Ss Gg' via NUnit-Discuss

Ss Gg

unread,
Jun 1, 2016, 12:06:56 PM6/1/16
to NUnit-Discuss
thanks, so then I would expect something like :

Assert.AreEqual(expectedData,rx,"error message",{0:x})
but this is incorrect. Could you please correct me ?

I found 2 different definition of the last parameter of the Assert :

1st definition:
public static void AreEqual(object expected, object actual, string message, params object[] args)
    Member of NUnit.Framework.Assert

Summary:
Verifies that two objects are equal. Two objects are considered equal if both are null, or if both have the same value. NUnit has special semantics for some object types.  If they are not equal an NUnit.Framework.AssertionException is thrown.

Parameters:
expected: The value that is expected
actual: The actual value
message: The message to display in case of failure
args: Array of objects to be used in formatting the message


2nd definition :

public static void AreEqual(
	
object expected,
	object actual,
	string
 message,
	params object[] parameters
)

Parameters

expected
Type: System.Object

The first object to compare. This is the object the unit test expects.

actual
Type: System.Object

The second object to compare. This is the object the unit test produced.

message
Type: System.String

A message to display if the assertion fails. This message can be seen in the unit test results.

parameters
Type: System.Object[]

An array of parameters to use when formatting message.




Dňa utorok, 31. mája 2016 17:58:25 UTC+2 charlie napísal(-a):

Ss Gg

unread,
Jun 1, 2016, 12:07:56 PM6/1/16
to NUnit-Discuss
thanks, so then I would expect something like :

Assert.AreEqual(expectedData,rx,"error message",{0:x})
but this is incorrect. Could you please correct me ? Thanks a lot for your explanation, I a m sorry about stupid questions.

I found 2 different definition of the last parameter of the Assert :

1st definition:
public static void AreEqual(object expected, object actual, string message, params object[] args)
    Member of NUnit.Framework.Assert

Summary:
Verifies that two objects are equal. Two objects are considered equal if both are null, or if both have the same value. NUnit has special semantics for some object types.  If they are not equal an NUnit.Framework.AssertionException is thrown.

Parameters:
expected: The value that is expected
actual: The actual value
message: The message to display in case of failure
args: Array of objects to be used in formatting the message


2nd definition :

public static void AreEqual(
	
object expected,
	object actual,
	string
 message,
	params object[] parameters
)

Parameters

expected
Type: System.Object

The first object to compare. This is the object the unit test expects.

actual
Type: System.Object

The second object to compare. This is the object the unit test produced.

message
Type: System.String

A message to display if the assertion fails. This message can be seen in the unit test results.

parameters
Type: System.Object[]

An array of parameters to use when formatting message.




Dňa utorok, 31. mája 2016 17:58:25 UTC+2 charlie napísal(-a):
Sure.. just use standard .NET string formatting as you would with

Charlie Poole

unread,
Jun 1, 2016, 12:17:39 PM6/1/16
to NUnit-Discuss
On Wed, Jun 1, 2016 at 9:06 AM, 'Ss Gg' via NUnit-Discuss
<nunit-...@googlegroups.com> wrote:
> thanks, so then I would expect something like :
>
> Assert.AreEqual(expectedData,rx,"error message",{0:x})

That is not valid C#! you would have to embed the formatting
expression in the formatting string, just as you do for any Write
statement. I suggest you craft a write statement that puts out what
you want to see, like...

Console.WriteLine("I thought I would get {0} but you gave me {1}!",
expected, actual);

Then use the same values for the third and following arguments to your
Assert statement.

> but this is incorrect. Could you please correct me ?
>
> I found 2 different definition of the last parameter of the Assert :

If you find conflicting information in the docs, please give us the URLs of the
two different versions. Otherwise, we can't fix it!

However, I don't see any difference between the two things you are quoting.
What is confusing you about them?

Charlie

Charlie Poole

unread,
Jun 1, 2016, 12:19:43 PM6/1/16
to NUnit-Discuss
As a further follow-up, are you looking at the docs for two different
versions of NUnit? Obviously, they may differ. You should only refer
to the docs of the version you are using. However, in this particular
matter, I don't think there will be any true difference.

Ss Gg

unread,
Jun 1, 2016, 1:06:05 PM6/1/16
to NUnit-Discuss
Hi,

1st : args: Array of objects to be used in formatting the message
2nd : An array of parameters to use when formatting message.

This looks to me as difference. array of objects vs. parameters

Sources:
https://msdn.microsoft.com/en-us/library/ms243429.aspx
nunit help 2.6.0


Console.WriteLine format is clear to me , but if I will use this as error message inside assert , than as output of the Assert i think i will see values 2 times, once in decimal format and then formatted in hexa inside the error message. 


Dňa streda, 1. júna 2016 18:19:43 UTC+2 charlie napísal(-a):

Charlie Poole

unread,
Jun 1, 2016, 1:20:51 PM6/1/16
to NUnit-Discuss
On Wed, Jun 1, 2016 at 10:06 AM, 'Ss Gg' via NUnit-Discuss
<nunit-...@googlegroups.com> wrote:
> Hi,
>
> 1st : args: Array of objects to be used in formatting the message
> 2nd : An array of parameters to use when formatting message.
>
> This looks to me as difference. array of objects vs. parameters

It's saying the same thing in different words. If you like, it's an array
of objects being used as parameters for the message. :-)

>
> Sources:
> https://msdn.microsoft.com/en-us/library/ms243429.aspx
> nunit help 2.6.0

I thought maybe you were using two different versions of NUnit docs,
but actually it's two different products from two different companies!
The first is for Microsoft's test framework. The second is for NUnit
2.6.0 - a rather old version.

>
> Console.WriteLine format is clear to me , but if I will use this as error
> message inside assert , than as output of the Assert i think i will see
> values 2 times, once in decimal format and then formatted in hexa inside the
> error message.

So repeat the argument twice. Notice that this is a params array.

I think I may be able to help you better if you explain your background.
Are you a C# programmer or are you used to some other language?

Charlie

Ss Gg

unread,
Jun 1, 2016, 2:20:30 PM6/1/16
to NUnit-Discuss
Thanks for an answers,

So the result is that I will always see original output from  assert in decadic format and then I can do my own output formatted ?

Not equal 02-00-00-00, 29-00-00-00  Expected and actual are both <System.Byte[4]>  Values differ at index [0]  Expected: 2  But was:  41


So there is no possiblity to format original assert output and see values just once ? I expected that I can use some format params to format original values, not the copies.





Dňa streda, 1. júna 2016 19:20:51 UTC+2 charlie napísal(-a):

Charlie Poole

unread,
Jun 1, 2016, 2:34:17 PM6/1/16
to NUnit-Discuss

As I explained in my first answer, you can only change that original output if you are able to override ToString for the type. That's not possible for bytes so change to nunit would be needed.

Richard Howells

unread,
Jun 1, 2016, 2:47:13 PM6/1/16
to nunit-...@googlegroups.com

Hi Charlie,

 

I’m guessing here, but I assume it would not be difficult to write your own custom Assert?  You could then make it do anything you wanted.  Right?

 

Best wishes,

 

Richard Howells

Ric...@dynamisys.co.uk

www.dynamisys.co.uk

 

(L) +44 (0) 1793 731225

(M) +44 (0) 7769 266522

Charlie Poole

unread,
Jun 1, 2016, 3:14:12 PM6/1/16
to NUnit-Discuss
Hi Richard,

The logic for how expected and actual values are displayed is embedded
in the base Constraint class from which individual Constraints, like
EqualConstraint, derive. Assert.That (and all other Asserts) simply
construct a constraint, call into it and get an error message from it.

So yes, you could write a custom Assert, but you would have to
duplicate every bit of functionality that's built into the constraint
system in order for it to work. Or you don't mind a horrible hack, you
could examine the constraint message, find lines starting with
"Expected:" and " but was:" and modify them.

If hex output should be something many folks want, it would best be
implemented as a new Modifier on the Constraint class. Modifiers are
bits of the fluent Syntax that change how certain constraints work,
like .IgnoreCase for example. You could have something like "Hex" or
"HexBytes" that changed how the message was generated. For that to
happen, we would need an issue with more than one person advocating
for its inclusion.

Charlie

Charlie Poole

unread,
Jun 1, 2016, 3:15:41 PM6/1/16
to NUnit-Discuss
Alternatively, I have considered in the past having a modifier that simply
causes the standard "Expected, etc." lines to be eliminated, allowing the
user complete control of the message.

Richard Howells

unread,
Jun 1, 2016, 3:20:52 PM6/1/16
to nunit-...@googlegroups.com
Ok - a little harder than I'd guessed.

Charlie Poole

unread,
Jun 1, 2016, 3:27:46 PM6/1/16
to NUnit-Discuss
Fairly simple as a part of NUnit - for an experienced NUnit dev
anyway. Quite difficult for a user to extend, 'cause we never
considered it as a possible extension before. :-)

Johan Appelgren

unread,
Jun 3, 2016, 4:24:54 AM6/3/16
to NUnit-Discuss
If you are using NUnit 3 I think you can use TestContext.AddFormatter to control how values are formatted.

https://github.com/nunit/docs/wiki/TestContext

Haven't used it myself though.

Charlie Poole

unread,
Jun 3, 2016, 6:18:25 AM6/3/16
to NUnit-Discuss

That's true... I forgot we had added that. If you use it in a OneTimeSetUp it will remain in effect for all tests under it.

Reply all
Reply to author
Forward
0 new messages