Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

What are your thoughts on this style of writing?

  • Specifically the “Explicit” way of defining arrange, act and assert scopes.
  • also, given the name of the test, where should the call to “OnApplyTemplate” be (if you’ve never seen the code)?
  • which version makes more sense to you? A, or B?

 

version A:

 

image

 

version B:

image

Published Tuesday, June 02, 2009 8:55 PM by RoyOsherove

Comments

Tuesday, June 02, 2009 11:26 PM by Pieter

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

I have not view on the explicit scopes either way really, other than it seems to add a lot of extra code for seemingly no benefit.

I believe that OnApplyTemplate should be part of Arrange (version B) as that makes more sense to me.

Tuesday, June 02, 2009 11:52 PM by Ayende Rahien

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

Ugh, that looks very ugly

Wednesday, June 03, 2009 12:05 AM by Nigel Sampson

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

I think I'd prefer B given the functionality of OnApplyTemplate. The explicit AAA syntax I'm not sure of, I think a simple "// Arrange" comment suffices.

Wednesday, June 03, 2009 12:22 AM by Mark Seemann

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

While I'm a proponent of being rather explicit about Four-Phase Test (or AAA, for that matter) (blogs.msdn.com/.../testmethod-code-snippet.aspx), I think comments fit the bill nicely.

Code should only appear in tests if it influences the outcome of the test. It's not clear to me whether those Arrange, Act and Assert scopes actually make a difference. If they do, it may be okay, but otherwise I don't think they should be there.

I don't know what OnApplyTemplate does, but it looks as though it's part of the test's Fixture, and if that's the case, it belongs in the Arrange scope.

Wednesday, June 03, 2009 1:06 AM by Kutch

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

Initial point: rename ctl to customControl or something else that makes more sense than "ctl". Grab a copy of Robert Martin's "Clean code" for a dozen reasons why :)

Your test wants to check whether the invocation was called, thus anything prior to core.FireEvent( ) is preparation, thus should be in the scope of Arrange.

Wednesday, June 03, 2009 3:18 AM by Mark Monster

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

Hmm, I like this explicit AAA-style.

Difficult to say what version is better. I think version B makes a little bit more sense because the OnApplyTemplate is part of the setup of the environment.

-

Mark Monster

Wednesday, June 03, 2009 3:29 AM by Peter Lillevold

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

Seems like the using scopes are some form of recording mechanism?

Anyways, my vote goes to B since it feels more of an arrange operation.

Wednesday, June 03, 2009 3:37 AM by Vagif Abillov

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

1. With all my respect to IDisposable this syntax looks like overkill. Why would you need using(SilverUnit.Assert)? What kind of clean-up will it trigger?

2. Judging from the name of the test, I'd put OnApplyTemplate in "arrange" section.

Wednesday, June 03, 2009 8:37 AM by Joshua Flanagan

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

I think a little whitespace between the AAA sections does the job just fine. Unnecessary code, and even comments, seem like overkill.

Even better, with the context-specification style, you move the arrange and act into you setup code and get an even clearer look at your assertions.

Wednesday, June 03, 2009 8:55 AM by Dean Nolan

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

I would pick B as it does seem to belong to the arrange scope and is better grouped with the other control template code.

Was this code just for to demo this kind of thing or is this from "real" code?

I think unit testing is very useful but I don't think it's nessessary to test that a mouse click changes the mouse state to pressed, that's kind of a given and means you wrote all the extra test code for nothing. Maybe I'm missing something though.

I would personally test the behaviour of what is to happen when the mouse is pressed, e.g save details or, fire weapon etc.

Wednesday, June 03, 2009 9:18 AM by Chris

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

I'm with the majority in that the using statements are a bit overkill, considering comments are already available to provide visual queues.  So unless they do more than that, I would vote no.

And isn't this just the kind of thing you railed against in your critique of the ASP.NET MVC unit tests?  :)

Wednesday, June 03, 2009 9:37 AM by Shane Courtrille

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

Noisy...

Wednesday, June 03, 2009 11:37 AM by Thomas Eyde

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

I have no opinion on the usings as I haven't done any Silverlight unit testing.

However, I usually put the Arrange-Act part in my Setup method, and keep all Asserts in my tests methods. I feel no need to mark my sections as one or the other.

Option B fits that pattern.

Another benefit from arranging tests this way, is that we can name our fixture after the user story or use case scenario, and the tests after each desired outcome.

Wednesday, June 03, 2009 11:41 AM by Doug McCall

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

The explicit organization of the code is appealing on one level especially when someone is not a seasoned user of a testing framework or when tests aren't working as expected. But, I'm also with the majority that the using statements may be too much.

I like version B because I view "assert" as a single, specific action and the OnApplyTemplate method feels like setup [arrange] to me.

Wednesday, June 03, 2009 12:50 PM by Eber Irigoyen

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

too much typing, aren't why trying to minimize code?

Wednesday, June 03, 2009 12:50 PM by ChrisB

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

1 vote for Version B

using statements would need to provide additional functionality otherwise comments would win

Wednesday, June 03, 2009 4:12 PM by robi y

# re: Using Explicit Arrange,Act,Assert scopes in tests – thoughts?

If A was correct, you're gathering two tests into one.

=> B