当前位置:网站首页>Unittest simple project
Unittest simple project
2022-07-07 09:14:00 【qq_ forty-five million five hundred and thirteen thousand nine 】
1. Business module
""" file name :calc.py"""
class Calc1:
def __init__(self, a, b):
self.a = a
self.b = b
def multiply(self):
x = self.a * self.b
return x
def division(self):
y = round(self.a / self.b, 2)
return y
if __name__ == '__main__':
print(Calc1(3,7).multiply())
print(Calc1(3,7).division())
2. unit testing
""" file name :testdiv.py"""
import unittest
from calc import *
class TestDiv(unittest.TestCase):
@classmethod
def setUpClass(cls):
print(" Preparation of all use cases ")
def setUp(self):
print('~' * 10, ' Beginning of the test ', '~' * 10)
def tearDown(self):
print('-' * 10, ' End of test ', '-' * 10)
@classmethod
def tearDownClass(cls):
print(" The closeout of all use cases ")
def test_div1(self):
print("1.2. Multiplication verification ")
self.assertEqual(30, Calc1(7, 5).multiply(), msg="testdiv Multiplication verification error ")
def test_div2(self):
print("2.2. Division verification ")
self.assertEqual(1.4, Calc1(7, 5).division(), msg="##")
if __name__ == '__main__':
unittest.main()
""" file name :testmul.py"""
import unittest
from calc import *
class TestMul(unittest.TestCase):
def setUp(self):
print('~' * 10, ' Beginning of the test ', '~' * 10)
def tearDown(self):
print('-' * 10, ' End of test ', '-' * 10)
def test_mul1(self):
print("1.1. Multiplication verification ")
self.assertEqual(100, Calc1(25, 4).multiply(), msg="##")
def test_mul2(self):
print("2.1. Division verification ")
self.assertEqual(6.0, Calc1(25, 4).division(), msg="testmul Division validation error ")
if __name__ == '__main__':
unittest.main()
3. Test the actuator
""" file name :runtest1.py"""
"""【 Purpose 】 Merge multiple classes ---> Test Suite """
import unittest
from testmul import TestMul
from testdiv import TestDiv
# HTMLTestRunner External import required
import HTMLTestRunner
if __name__ == '__main__':
"""【1. Different classes are written into their respective test suites 】"""
testsuite_1 = unittest.TestLoader().loadTestsFromTestCase(TestMul)
testsuite_2 = unittest.TestLoader().loadTestsFromTestCase(TestDiv)
"""【2. Multiple test suites are combined into a large test suite 】"""
testsuite_all = unittest.TestSuite([testsuite_1, testsuite_2])
"""【3. The name of the test report 】 Write binary """
html1 = open('testreport.html', 'wb')
htmlrunner = HTMLTestRunner.HTMLTestRunner(stream=html1, title=' Multiplication and division test report ')
"""【4. Actuator execution test suite 】"""
htmlrunner.run(testsuite_all)
html1.close()
""" file name :runtest2.py"""
"""【 Purpose 】 Merge multiple module files in the specified directory ---> Test Suite """
import unittest
if __name__ == '__main__':
"""【1】 Declare the location of the directory """
test_dir = "./"
"""【2】 Find the directory with 'test' The first module file """
test_file = "test*.py"
"""【3】 Merge multiple module files into a test suite """
test_suite = unittest.defaultTestLoader.discover(test_dir, pattern=test_file)
"""【4】 Actuator execution test suite """
test_run1 = unittest.TextTestRunner()
test_run1.run(test_suite)
Run the actuator
1. menu bar : function ---> function
2. Terminal command line :
python file .py
python3 file .py
Test report
Templates
import unittest
class TestSample(unittest.TestCase):
def setUp(self) :# preparation , Each use case executes
print("~~~~~~~ Start testing ~~~~~~~")
def tearDown(self):# Finishing work , Each use case executes
print("------ End of test ------")
def test_1(self):# The test case is based on test_ Lead
self.assert(" Expected value "," class ( Parameters 1, Parameters 2). Method ()",msg=" Show... On failure ")
def test_2(self):# The test case is based on test_ Lead
self.assert(" Expected value "," class ( Parameters 1, Parameters 2). Method ()",msg=" Show... On failure ")
if __name__ == '__main__':# Test the actuator
unittest.main()
边栏推荐
- What is the value of getting a PMP certificate?
- Unityshader introduction essentials personal summary -- Basic chapter (I)
- ChaosBlade:混沌工程简介(一)
- C语言指针(下篇)
- Upgrade Alibaba cloud RDS (relational database service) instance to com mysql. jdbc. exceptions. Troubleshooting of jdbc4.communicationsexception
- Mysql数据库-锁-学习笔记
- Chaosblade: introduction to chaos Engineering (I)
- How to use Arthas to view class variable values
- Port occupation troubleshooting
- Full link voltage test of the e-commerce campaign Guide
猜你喜欢
Jemter operation
2022-07-06 Unity核心9——3D动画
Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]
Unityshader introduction essentials personal summary -- Basic chapter (I)
Goldbach conjecture C language
Panel display technology: LCD and OLED
Cesium load vector data
硬核分享:硬件工程师常用工具包
Entity of cesium data visualization (Part 1)
Run can start normally, and debug doesn't start or report an error, which seems to be stuck
随机推荐
Golang etcdv3 reports an error. The attribute in grpc does not exist
Pytest+request+allure+excel interface automatic construction from 0 to 1 [five nails / flying Book notice]
Locust performance test 3 (high concurrency, parameter correlation, assembly point)
Unity Shader入门精要初级篇(一)-- 基础光照笔记
Interpretation of MySQL optimization principle
Leetcode question brushing record (array) combination sum, combination sum II
Unity shader beginner's Essentials (I) -- basic lighting notes
Jemter operation
systemd
Detailed learning notes of JVM memory structure (I)
2020 year end summary
Common operating commands of Linux
Entity of cesium data visualization (Part 1)
C语言指针(下篇)
Personal deduction topic classification record
[chaosblade: delete pod according to the tag, pod domain name access exception scenario, pod file system i/o failure scenario]
徽商期货公司评级是多少?开户安全吗?我想开户,可以吗?
Self awakening from a 30-year-old female programmer
使用Typora编辑markdown上传CSDN时图片大小调整麻烦问题
Do you have any certificates with high gold content?