Importance of unit test - not going to cover in this discussion.
The article is primarily focused on importance of structuring a unit test.
In my 4 years of unit testing experience the biggest challenge I have faced is in terms of writing an "almost" production quality unit tests. It gets important because the structure and quality of unit test derives how far the unit tests would be maintained!
Historically, I have been naming a unit test method by describing the logic that uni test is going to assert. That helped, but then the test was not just doing assertion... the method was also responsible for all three A's (AAA) Arrange-Act-Assert. Which kind of resulted in coupling and went against the DRY principle. We always need the same Arrange-Act across different unit tests, which was repetitive.
Found the following simple approach to arrange unit test in one of the Pluralsight videos:
public abstract class GivenWhenThen{
[SetUp]
public void Init(){
Given();
When();
}
public abstract void Given();
public virtual void When(){}
}
Then, I use the following protocol to structure my tests:
Given = Namespace
When = Class
Then = Test method.
using Given_Order_Service{
//Sample Implementation:
public class When_an_empty_order_is_placed{
private IOrderService _orderService;
public override void Given(){
_orderService = new OrderService():
}
public override void When(){
_orderService.PlaceOrder(null);
}
[Test]
[ExpectedException(typeof(EmptyBasketException)]
public void Then_Exception_Is_thrown_with_Empty_basket_message(){
//assertion..
}
}
}
The article is primarily focused on importance of structuring a unit test.
In my 4 years of unit testing experience the biggest challenge I have faced is in terms of writing an "almost" production quality unit tests. It gets important because the structure and quality of unit test derives how far the unit tests would be maintained!
Historically, I have been naming a unit test method by describing the logic that uni test is going to assert. That helped, but then the test was not just doing assertion... the method was also responsible for all three A's (AAA) Arrange-Act-Assert. Which kind of resulted in coupling and went against the DRY principle. We always need the same Arrange-Act across different unit tests, which was repetitive.
Found the following simple approach to arrange unit test in one of the Pluralsight videos:
public abstract class GivenWhenThen{
[SetUp]
public void Init(){
Given();
When();
}
public abstract void Given();
public virtual void When(){}
}
Then, I use the following protocol to structure my tests:
Given = Namespace
When = Class
Then = Test method.
using Given_Order_Service{
//Sample Implementation:
public class When_an_empty_order_is_placed{
private IOrderService _orderService;
public override void Given(){
_orderService = new OrderService():
}
public override void When(){
_orderService.PlaceOrder(null);
}
[Test]
[ExpectedException(typeof(EmptyBasketException)]
public void Then_Exception_Is_thrown_with_Empty_basket_message(){
//assertion..
}
}
}
No comments:
Post a Comment