Introduction to test cases (2) - Writing test cases using the equivalence class division method

  • Equivalence class division is an important method in test case method design, and equivalence class division is even needed in other use case design methods.
  • This method has been around for a long time, and many people have written about this method. This article introduces code to illustrate how this method works.
    The code examples in this article are all written in Python

1. The meaning of equivalence class division method

Reduce a massive (infinite) set of test cases to a minimum in steps, but the process is equally effective.

2. Principle of equivalence class division method

- 1. First of all, what is an equivalence class?

等价类就是“他大舅、他二舅、都是他舅,高桌子、低板凳都是木头。”

- 2. So what is the equivalence class division method?

而等价类划分法就是:假设要找他舅,那只要叫他其中一个舅舅来就行了,不需要把两个舅舅都喊来。

- 3. Use code (Python) to demonstrate equivalence class division

  • Assume that the method find_uncle (find uncle) is the application under test.

    def find_uncle(uncle):
        # 这是一个用来找舅舅的方法
        if uncle['type'] == 'uncle':
        	# 只要数据内的“type”值是uncle,就输出“找到舅舅”
            print('找到舅舅了')
    
  • Create two variables as his first uncle and second uncle

    uncle_older = {
          
          'type': 'uncle', 'age': 45} # 这条数据代表大舅
    uncle_junior = {
          
          'type': 'uncle', 'age': 40} # 这条数据代表二舅
    
  • Run this code snippet

    print("这时来的是大舅:") # 打印日志
    find_uncle(uncle_older) # 输入“大舅”数据,并运行find_uncle方法
    print("这时来的是二舅:") # 打印日志
    find_uncle(uncle_junior)# 输入“二舅”数据,并运行find_uncle方法
    
  • The result of running the code, view the output:
    image.png

  • At this time, no matter whether it was the eldest uncle or the second uncle, they all found their uncle.
    In fact, there are two pieces of data. No matter which one is used, we have completed the test of this program.

- 4. This is the equivalence class division method:

Not all data needs to be tested, but similar inputs, similar outputs, and similar operations should be grouped into a test group to reduce test cases while still being enough to cover the test objects.

  • In fact, in the above test to find uncle, we should also design a test data, that is, the case where "type" is not equal to "uncle" , and input this data into the method, "Uncle found" will not be output , so Only then can we prove that the judgment condition of this method "find uncle" is correct**. Of course, this does not belong to the equivalence class division method, and will be introduced in detail later.

So what if this method is utilized in use case design? Give a practical example:

  • Suppose you add products to the shopping cart function in a test mall. This mall sells two types of goods, one is physical goods and the other is virtual goods.
  • So when we design the use case, do we need to design a use case that adds all the products in the mall to the shopping cart?
    Obviously no, you only need to design a use case for adding physical goods and a use case for virtual goods.

3. The impact of different conditions on the division of equivalence classes

  • Equivalence class division is a very important method for testing. However, when choosing equivalence class division, it means choosing to take risks, because there may be a risk of missing tests due to the reduction of test cases. So be careful when choosing a category.

  • Let’s still use the above method of finding an uncle as an example:
    Assume that the selection judgment conditions have changed, and we need to find uncles who are older than 40 years old, including 40 years old.

    def find_uncle(uncle):
        # 这是一个用来找舅舅的方法
        if uncle['type'] == 'uncle' and uncle['age'] >= 40:
        	# 数据内的“type”值是uncle,并且“age”要大于等于40,输出“找到舅舅”
            print('找到舅舅了')
    
  • So at this time, are the two uncles still equivalent classes?

  • the answer is negative. 'age' is greater than or equal to 40 years old actually contains two judgment conditions, one is greater than 40, the other is exactly equal to 40, and of the two uncles, one is exactly 40 and the other is 45.

  • Rewrite the code to make the two judgment conditions more intuitive, and add three situations for inputting uncle data.

    def find_uncle(uncle):
        # 这是一个用来找舅舅的方法
        if uncle['type'] == 'uncle':
            if uncle['age'] > 40:
                print('找到大于40岁的舅舅了')
            elif uncle['age'] == 40:
                print('找到等于40岁的舅舅了')
    
    print("这时来的是大舅:")
    find_uncle(uncle_older) # 输入“大舅”数据,并运行find_uncle方法
    print("这时来的是二舅:")
    find_uncle(uncle_junior) # 输入“二舅”数据,并运行find_uncle方法
    print("大舅和二舅一起来了")
    find_uncle(uncle_older) # 输入“大舅”数据,并运行find_uncle方法
    find_uncle(uncle_junior) # 输入“二舅”数据,并运行find_uncle方法
    
  • Running the code, you can see that only when the uncle and the second uncle come together, all the judgment conditions can be tested.
    image.png

  • Therefore, in this case, the first uncle and the second uncle cannot be divided into an equivalence class.

5. Finally

  • Let’s review again the meaning of equivalence class partitioning:
    reduce the massive (infinite) set of test cases to a minimum in steps, but the process is equally effective.

  • The equivalence class division method is one of the common methods in testing methods. Reducing test cases is risky, but you should not give up because sometimes the amount of data is very large, or even infinite, and complete testing is impossible. What we need to do is to judge the division conditions more carefully, rather than giving up on equivalence class division directly. Therefore, even if there are risks, we still need to use the equivalence class division method during testing to reduce the amount of testing and improve testing efficiency.

Appendix: [Test case entry series catalog]

Getting Started with Test Cases (1) - How to write a test case title, preconditions, test steps and expected results?
Getting Started with Test Cases (2) - Using Equivalence Classification to Write Test Cases
Getting Started with Test Cases (3) - Using Boundary Value Analysis to Write Test Cases
Getting Started with Test Cases (4) - Using Process Analysis and State Analysis to Write Test
Cases Getting Started with Use Cases (5) - How to Conduct Requirements Analysis
Getting Started with Test Cases (6) - Summarizing test points based on test type and granularity

————————————————————————————————————
The public account of the same name of the blog I run [Essential Skills for Software Testing] will be held from time to time Upload test-related information and collect it if necessary.
Insert image description here
If necessary, please click on the QR code below the article to get it~Insert image description here

Guess you like

Origin blog.csdn.net/weixin_40883833/article/details/126651881