Questions tagged [arrange-act-assert]

A pattern for structuring a unit test.

This pattern divides each unit test into three sections, separated by blank lines:

  1. Arrange all necessary preconditions and inputs.
  2. Act on the object or method under test.
  3. Assert that the expected results have occurred.

Organizing tests in this way makes them easier to read and understand.

The pattern was observed and named by Bill Wake in 2001.

"Arrange-Act-Assert syntax" or "AAA syntax" also refers to a Rhino Mocks syntax which supports this pattern.

41 questions
102
votes
14 answers

Should it be "Arrange-Assert-Act-Assert"?

Regarding the classic test pattern of Arrange-Act-Assert, I frequently find myself adding a counter-assertion that precedes Act. This way I know that the passing assertion is really passing as the result of the action. I think of it as analogous…
Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
21
votes
2 answers

Are heading comments recommended when unit testing with Arrange-Act-Assert?

I find the concept of partitioning the statements of my unit tests as suggested in the AAA pattern useful. I tend to add heading comments so that the tests look like this: // Arrange int a = 1; int b = 2; // Act int c = a + b; //…
Lea Hayes
  • 62,536
  • 16
  • 62
  • 111
17
votes
5 answers

Arrange Act Assert Alternatives

The general question is are there alternative patterns to AAA for unit testing? If, yes, would be very interesting to see some examples and hear about their pros and cons. And as the simplest example of AAA test (in c#, using var for the sake of…
Arman
  • 5,136
  • 3
  • 34
  • 36
13
votes
2 answers

How to use the AAA syntax to do an AssertWasCalled but ignore arguments

I'm using the new AAA syntax and wanted to know the syntax to do the below and have the mock ignore the arguments: mockAccount.AssertWasCalled(account => account.SetPassword("dsfdslkj")); I think the below is how I would do this with the record/…
Toran Billups
  • 27,111
  • 40
  • 155
  • 268
11
votes
4 answers

Rhino Mocks AAA Quick Start?

I've been looking around for some decent information on using Rhino Mocks 3.5+ with the AAA syntax. I find a lot of blogs that have a mix of things from the old and new which seem to make it more difficult to figure out how to use it. Would would be…
Bill Campbell
  • 2,413
  • 6
  • 27
  • 32
10
votes
1 answer

TDD Arrange Act Assert pattern when using Mocks to verify dependency calls

I'm using Moq to test behaviour of some void methods. Using MockBehaviour.Strict every call to the mock must be specified during Arrange step. This is resulting in a lot of tests not having any Assert (or Verify) step. The pass condition is simply…
fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253
8
votes
5 answers

What is the correct way to report an error in a Python unittest in the setUp method?

I've read some conflicting advice on the use of assert in the setUp method of a Python unit test. I can't see the harm in failing a test if a precondition that test relies on fails. For example: import unittest class MyProcessor(): """ …
Jon Cage
  • 36,366
  • 38
  • 137
  • 215
8
votes
5 answers

Is there value in unit testing auto implemented properties

It seems exceptionally heavy handed but going by the rule anything publicly available should be tested should auto-implemented properties be tested? Customer Class public class Customer { public string EmailAddr { get; set; } } Tested…
ahsteele
  • 26,243
  • 28
  • 134
  • 248
6
votes
5 answers

Best approach towards applying the Arrange-Act-Assert pattern when expecting exception

I'm trying to follow the Arrange-Act-Assert pattern when writing unit test and I got to a point where I'm confused on which approach will be better. I'm using xUnit and my first approach to the problem was: //Arrange int key = 1; string value =…
pmbanugo
  • 303
  • 3
  • 12
4
votes
2 answers

Extraction of data in unit test AAA pattern

In "AAA" pattern where extraction of the act data should be written? In the Act or in the Assert section? Consider this Unit Test, the extraction of the two persons, should it be in the Act as in the example or in the Assert? we would like to…
gdoron
  • 147,333
  • 58
  • 291
  • 367
4
votes
2 answers

Mixing Assert and Act in AAA unit testing syntax

Is it OK to mix Assert and Act steps? Is AAA more of a guideline than a rule? Or am I missing something? Here is my test: [TestMethod] public void CancelButtonSelected_DontCancelTwiceThenCancel_DialogCloses() { // Arrange IAddAddressForm…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
4
votes
1 answer

What's the difference between Arrange and Act in the Arrange, Act, Assert pattern?

I don't really understand the difference between the arrange and act part of the pattern in unit tests. Does Arrange ONLY mean the creation of the objects? Why should we separate the Arrange from the Act part in the first place, and what's the…
cobby
  • 484
  • 3
  • 18
3
votes
2 answers

In the Arrange-Act-Assert test style, is it OK to move the Act into a beforeEach/setUp block?

When unit testing the result and/or side effects from a single logical action, how do you keep the code DRY? Take the following example written in Jasmine: describe("frobnicate", function () { var foo; beforeEach(function () { foo =…
Michael Kropat
  • 14,557
  • 12
  • 70
  • 91
3
votes
1 answer

Rhino Mocks -- assert no interaction with mock/stub

Is it possible to tell that a mock/stub has seen no interaction at all in RhinoMocks. Something along the lines of: logger.AssertNoInteraction(); Which would assert no method has been called on the stubbed logger. This would be a much less tedious…
grootjans
  • 592
  • 1
  • 6
  • 17
2
votes
1 answer

Rhino Mocks: AssertNotCalled (except when I'm loading)

I have a dependency that gets called when the object I'm testing is created. However, it should never be called after that. How would I write such a test? I'd like just this line as my test (since I'm trying to follow the AAA style of test writing).…
Jeff B
  • 8,572
  • 17
  • 61
  • 140
1
2 3