Home > Database > Mysql Tutorial > body text

How to perform automated unit testing with Python Unittest

php中世界最好的语言
Release: 2018-04-09 17:53:44
Original
3341 people have browsed it

This time I will bring you Python How Unittest performs automated unit testing Unit testing, what are the precautions for Python Unittest to perform automated unit testing, the following are Let’s take a look at practical cases.

The example in this article shares the specific code of the Python Unittest automated unit testing framework for your reference. The specific content is as follows

1. python testing framework (This article only covers PyUnit)

Reference address

2. Environment preparation

First make sure that Python is installed, and then install PyUnit. The Python version is relatively new. Integrated with PyUnit (PyUnit provides a graphical test interface UnittestGUI.py)
Reference: View address

3, code example

The IDE used is PyCharm, The DEMO structure is as shown in the figure

1. A simple example

# Test002_Fail.py
# -*- coding:utf-8 -*-
import unittest
class Test002_Fail(unittest.TestCase):
  #测试用例前执行
  def setUp(self):
    print 'Case Before'
    pass
  #测试用例后执行
  def tearDown(self):
    print 'Case After'
    pass
  #测试用例1
  def test_Case1(self):
    a = 3
    b = 2
    self.assertEqual(a+b,4,'Result Fail')
  #测试用例2
  def test_Case2(self):
    a = 2
    b = 3
    self.assertEqual(a*b,7,'Result Fail')
if name == 'main':
  unittest.main()
Copy after login

2. When there are many cases, you can use Suite to manage multiple cases. Suite can be regarded as a case container, which can hold many cases

# SuiteDemo.py
# -*- coding:utf-8 -*-
#一个类,添加测试集
import unittest
class SuiteDemo(unittest.TestCase):
  #测试用例前执行
  def setUp(self):
    print 'Case Before'
    pass
  #测试用例后执行
  def tearDown(self):
    print 'Case After'
    pass
  def test_Case1(self):
    a = 3
    b = 2
    self.assertEqual(a+b,5,'Result Fail')
    print 'Case1'
  def test_Case2(self):
    a = 2
    b = 3
    self.assertEqual(a*b,6,'Result Fail')
    print 'Case2'
#定义一个测试集合,方便添加Case
def suite():
  suiteTest = unittest.TestSuite()
  suiteTest.addTest(SuiteDemo("test_Case1"))
  suiteTest.addTest(SuiteDemo("test_Case2"))
  return suiteTest
#默认运行时通过 Suite 运行
if name == 'main':
  unittest.main(defaultTest='suite')
Copy after login

3.Suite and Case can be written in different Python files separately, which makes it easier to distinguish the case module and the Suite management module during testing. You only need to add case in the case module, and then add it to the Suite module.

case module

# TestCaseDemo
# -*- coding:utf-8 -*-
import unittest
class TestCaseDemo(unittest.TestCase):
  def setUp(self):
    pass
  def tearDown(self):
    pass
  def test_Case1(self):
    print 'test_Case1'
  def test_Case2(self):
    print 'test_Case2'
  def test_Case3(self):
    print 'test_Case3'
  def test_Case4(self):
    print 'test_Case4'
Copy after login

The next are three Suite modules, all of which import the Case module first, and then Add a Suite, then add a Case in the Suite. You need to use TestRunner during final execution. This TestRunner is equivalent to the executor of the Suite. Add Case directly in the main method, and then execute

# TestCase_Demo1.py
# -*- coding:utf-8 -*-
#集合写入一个方法,main调用并启动
import unittest
from TestCase_Demo import TestCaseDemo
#添加一个测试集合,并添加Case
def suite():
  suiteTest = unittest.TestSuite()
  suiteTest.addTest(TestCaseDemo('test_Case1'))
  return suiteTest
#指定并启动测试集合,运行集合方法
if name == 'main':
  runner = unittest.TextTestRunner()
  runner.run(suite())
Copy after login

to add different Cases to different Suites. Similarly, you can group Suite

to distinguish different Cases. , and Suite can also contain

# TestCase_Demo2.py
# -*- coding:utf-8 -*-
#集合可以写在main
import unittest
from TestCase_Demo import TestCaseDemo
#指定并启动测试集合
if name == 'main':
  #添加测试集合Case,并启动
  suiteTest = unittest.TestSuite()
  suiteTest.addTest(TestCaseDemo('test_Case1'))
  suiteTest.addTest(TestCaseDemo('test_Case2'))
  #直接启动集合
  runner = unittest.TextTestRunner()
  runner.run(suiteTest)
Copy after login
4. Summary

PyUnit is a unit testing framework for Python, which can be used to easily manage automated running cases. In Python , each method as long as the method name starts with test_ is regarded as a use case and can be regarded as a case. Here we introduce the use of Suite to manage Case, and run Case in groups, as well as many other functions. Please refer to: unittest — Unit testing framework

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Detailed explanation of the steps for using unittest test interface in python

How to count the number of occurrences of letters in Python

The above is the detailed content of How to perform automated unit testing with Python Unittest. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template