Carry on (Unit) Testing: Attributes

Before we get started with writing our test code, let’s discuss a few important little words and phrases. You will see these pop up in any examples of unit testing that you come across.

These are the phrases and words which are in the square brackets. And after I spent an hour debugging a test project that was not running two tests out of three, I realised how vital these attributes are.

Mainly these attributes tell your IDE what each section is doing.

In this screenshot there are four attributes:

[TestClass]

This one is not necessarily needed any more to run a test project but for the purposes of this post, this attribute labels the beginning of a class which contains the tests.

[SetUp]

As the label suggests, this is where the developer will create the environment for the tests to work in. This can be done separately to the individual tests or within the test method. Remember if you decide to do the set up within each test method then you would not require this attribute.

Another thing to note is that there is [SetUp] which will run for each method, or [OneTimeSetUp] which will only run once for each class of tests.

[TearDown]

This attribute is essentially house keeping. Once the tests are run then the environment which was created in the setup is cleared up. The same as the SetUp attribute, there is also a [OneTimeTearDown] attribute which can alternatively be used.

Finally we reach the main and vital attribute:

[Test]

This is where we write each test. And I say “each” because you have to include this with each individual test method that you write.

Remember when I mentioned the hour that I spent debugging my test project? This was the pesky thing that I forgot to include. I had included it on my first test method but then ended up forgetting to add it for the two others!

There we have it. A handful of attributes which will be required for the unit tests which we will start writing tomorrow!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: