python interface automated testing-unittest-batch use case management

 There must be more than one interface test case in our daily projects. How do we manage these batch cases when there are more and more cases? How to ensure that cases are not repeated? How to ensure the efficiency of case execution when there are very many cases (hundreds, thousands, or even more)? How to manage (batch) test data? How to separate data and script?

  The above issues are what we should focus on in automated testing. A single use case is actually not difficult.

Let’s take a look at how batch cases are managed in the unittest framework:


1. Manual loading batch use cases:


# -*- coding:utf-8 -*-
# 批量用例执行--手工加载

import unittest

class TestOne(unittest.TestCase):
    def setUp(self):
        print '\ncases before'
        pass

    def test_add(self):
        '''test add method'''
        print 'add...'
        a = 3 + 4
        b = 7
        self.assertEqual(a, b)

    def test_sub(self):
        '''test sub method'''
        print 'sub...'
        a = 10 - 5
        b = 5
        self.assertEqual(a, b)

    def tearDown(self):
        print 'case after'
        pass

if __name__ == '__main__':
    # 1、构造用例集
    suite = unittest.TestSuite()

    # 2、执行顺序是安加载顺序:先执行test_sub,再执行test_add
    suite.addTest(TestOne("test_sub"))
    suite.addTest(TestOne("test_add"))

    # 3、实例化runner类
    runner = unittest.TextTestRunner()

    # 4、执行测试
    runner.run(suite)

2. Automatic loading batch use cases:


# -*- coding:utf-8 -*-
# 批量用例执行--自动加载
import unittest
import os

class TestOne(unittest.TestCase):
    def setUp(self):
        print '\ncases before'
        pass

    def test_add(self):
        '''test add method'''
        print 'add...'
        a = 3 + 4
        b = 7
        self.assertEqual(a, b)

    def test_sub(self):
        '''test sub method'''
        print 'sub...'
        a = 10 - 5
        b = 5
        self.assertEqual(a, b)

    def tearDown(self):
        print 'case after'
        pass

if __name__ == '__main__':
    # 1、设置待执行用例的目录
    test_dir = os.path.join(os.getcwd())

    # 2、自动搜索指定目录下的cas,构造测试集,执行顺序是命名顺序:先执行test_add,再执行test_sub
    discover = unittest.defaultTestLoader.discover(test_dir, pattern='test_*.py')

    # 实例化TextTestRunner类
    runner = unittest.TextTestRunner()

    # 使用run()方法运行测试套件(即运行测试套件中的所有用例)
    runner.run(discover)

The above only solves the problem of how to manage batch cases. How to manage other problems (batch) test data? How to separate data and script? Will be introduced later.

[Introduction to Interface Automation Testing] Implement the interface automation testing framework from 0 to 1

Guess you like

Origin blog.csdn.net/ada4656/article/details/135049090