当前位置:网站首页>Unit test Chapter6
Unit test Chapter6
2022-07-27 05:00:00 【Rookie Xiaoxiao】
Chapter6
This chapter covers :
1. Compare the style of unit testing
2. The relationship between function and hexagonal architecture
3. Transition to output based testing
The first 4 Chapter introduces the four attributes of good unit testing : Prevent a return , Resistance to reconfiguration , Fast feedback and maintainability . These attributes form the reference frame , It can be used to analyze specific test and unit test methods . In the 5 In the chapter , We analyzed such a method : The use of simulation .
In this chapter , I apply the same reference framework to the theme of unit test style . There are three styles : Based on output , State based and communication based testing . In these three ways , Output based styles will produce the highest quality tests , State based testing is the second best choice , And communication based testing should be used only occasionally .
Unfortunately , You cannot use output based test styles everywhere . It only applies to code written in a purely functional way . But don't worry There are some techniques that can help you transform more tests into output based styles . So , You need to use functional programming principles to reorganize the basic code into a functional architecture .
Please note that , This chapter does not delve into the topic of functional programming . For all that , Hopefully, by the end of this chapter , You can have an intuitive understanding of the relationship between functional programming and output based testing . You will also learn how to write more tests using output based styles , And the limitations of functional programming and functional architecture .
6.1 Three styles of unit testing
As I mentioned in the introduction of this chapter , There are three styles of unit testing :
Output based testing
State based testing
Communication based testing
You can use a , Two or even all three styles . This section is defined by ( With examples ) These three unit test styles , Lay the foundation for the whole chapter . after , You will see how they score each other in the following sections .
6.1.1 Define output based styles
The first style of unit testing is based on output , In this style , You provide input to the system under test (SUT) And check its output ( chart 6.1). This type of unit test is only applicable to code that does not change the global or internal state , Therefore, the only component that needs to be verified is its return value .
The following listing shows an example of such code and the tests that cover it . PriceEngine Class accepts a range of products and calculates discounts .
public class PriceEngine
{
public decimal CalculateDiscount(params Product[] products)
{
decimal discount = products.Length * 0.01m;
return Math.Min(discount, 0.2m);
}
}
[Fact]
public void Discount_of_two_products()
{
var product1 = new Product("Hand wash");
var product2 = new Product("Shampoo");
var sut = new PriceEngine();
decimal discount = sut.CalculateDiscount(product1, product2);
Assert.Equal(0.02m, discount);
}
PriceEngine Multiply the number of products by 1%, And the upper limit of the result is 20%. There is nothing else in this class . It will not add products to any internal collections , It will not be saved in the database . CalculateDiscount() The only result of the method is the discount it returns : Output value ( chart 6.2).
The output based unit test style is also called function . The name is rooted in functional programming , Functional programming is a programming method that emphasizes code without side effects . We will discuss functional programming and functional architecture in detail later in this chapter .
6.1.2 Define state based styles
The state based style is about how to verify the state of the system after the operation is completed ( chart 6.3). The word "state" in this test method can refer to SUT In itself , One of its collaborators or out of process dependencies ( For example, a database or a file system ) The state of .
This is a state based test example . Order Class allows customers to add new products .
public class Order
{
private readonly List<Product> _products = new List<Product>();
public IReadOnlyList<Product> Products => _products.ToList();
public void AddProduct(Product product)
{
_products.Add(product);
}
}
[Fact]
public void Adding_a_product_to_an_order()
{
var product = new Product("Hand wash");
var sut = new Order();
sut.AddProduct(product);
Assert.Equal(1, sut.Products.Count);
Assert.Equal(product, sut.Products[0]);
}
This test verifies the product set after adding . With the list 6.1 The output based test examples in are different ,AddProduct() The result is a change to the order status .
6.1.3 Define a style based on communication
Last , The third type of unit testing is communication based testing . This style uses simulation to verify the communication between the tested system and its collaborators ( chart 6.4).
The following listing shows a communication based test example .
[Fact]
public void Sending_a_greetings_email()
{
var emailGatewayMock = new Mock<IEmailGateway>();
var sut = new Controller(emailGatewayMock.Object);
sut.GreetUser("[email protected]");
emailGatewayMock.Verify(
x => x.SendGreetingsEmail("[email protected]"),
Times.Once);
}
边栏推荐
猜你喜欢
随机推荐
Basic configuration of static routing to achieve network wide accessibility
JS day 2 (variables, variable usage, naming rules, syntax extensions)
【AtCoder Beginner Contest 260 (A·B·C)】
CEPH operation
[C language] detailed explanation of user-defined types (structure + enumeration + Union)
[C language] dynamic memory management
[error reporting] cannot read property 'parsecomponent' of undefined
Vscode opens a new chapter in the visualization of pull request update code branches
Two way republication experiment
Solution: how to use bash batch command in win10
消防安全培训资料汇总
Shift right of negative numbers
.htaccess learning
Structural mode - bridging mode
Stm32 cubemx hal----- PWM - change frequency
Digital integrated circuit: MOS tube device chapter (II)
博云容器云、DevOps 平台斩获可信云“技术最佳实践奖”
Network protocol details: IP
State Hook
【牛客讨论区】第七章:构建安全高效的企业服务








