Maintainability is of major importance in software development. Usually writing tests against your codebase increases maintainability in that it gives you some degree of freedom in making changes, at the same time assuring you did not break any existing logic. But, what needs to be kept in mind is that tests need to be maintained as well and that point should not be underestimated. Therefore, paying attention in writing maintainable tests is a very important ingredient for getting the most value out of your automated test suite (and for boosting your development speed).So, what are the major points of importance to achieve maintainable tests?
Actually there are a bunch of them like
- structuring tests according to the AAA principle (Arrange, Act, Assert) or
- testing a single scenario per test case.
.....and you'd like to verify that it throws an exception if the
public bool DoSomeInterestingStuff(MyStuff theStuff)
if(theStuff.IsInteresting == false)
throw new ThisIsBoringStuffException();
//start doing the really interesting stuff here
IsInterestingis not set to true, then you should only set that variable in your test setup, like
[TestMethod]The test setup should just contain the setting of the
public void TestDoSomeInterestingStuff_StuffIsNotInteresting_ShouldThrowException()
MyStuff someBoringStuff = new MyStuff
IsInteresting = false;
MyStuffmight have other properties as well. This assures that your test remains readable and clean. Someone else will immediately perceive the main intention of your test and moreover it remains maintainable as you don't have to modify your test setup in case you remove or add other properties to