C语言部分:
#Project.h
#pragma once
_declspec(dllexport) int sum(int a, int b);
#Project.c
#include"stdio.h"
_declspec(dllexport) int sum(int a, int b)
{
return a + b;
}
通过CS2022编译成动态链接库Project.dll(win,linx:os)文件
Python代码部分:
import unittest
from ctypes import *
from BeautifulReport import BeautifulReport# 导入BeautifulReport库
import ddt
def Generate_reports():
load_case = unittest.TestLoader().loadTestsFromTestCase(MyTestCase) # 加载example1类下的所有用例
# ------------------------test suit部分--------------------------
suite = unittest.TestSuite() # 构建测试套件
suite.addTest(load_case) # 将test_cases列表添加到suite中
# ------------------------生成测试报告-----------------------------
BeautifulReport(suite).report(filename='测试报告文件名称', description='测试报告标题', report_dir='.') # report_dir='.'把report放到当前目录下
@ddt.ddt
class MyTestCase(unittest.TestCase):
@classmethod
def setUpClass(self):
"""
前置,执行用例/类执行前or后执行
@classmethod
def setUpClass(cls):类前置函数,每个测试类运行前会自动执行一次该函数
def tearDownClass(cls):类后置函数,每个测试类运行后会自动执行一次该函数
def setUp(self):前置函数,每个用例运行前会自动执行一次该函数
def tearDown(self):后置函数,每个用例运行后会自动执行一次该函数
:return:
"""
# 加载库
self.adder = cdll.LoadLibrary('./Project.dll')
def test_1(self):
"""
测试用例
该处可填用例的相关描述
"""
#调用C动态库里的函数
res_int = self.adder.sum(4, 5)
#断言功能,判断期待值与结果值是否相等,否则输出备注
self.assertEqual(9, res_int, "结果等于" + str(res_int))
# -------------------------参数化的参数-------------------
test_data = [1, 2, 3, 4, 5]
@ddt.data(*test_data)
def test_2(self, data):
"""
参数化参数
"""
res_int = self.adder.sum(data, 1)
self.assertEqual(5, res_int, "结果等于" + str(res_int))
@unittest.skip("reason")
def test_3(self):
"""
跳过用例不执行:
unittest.skip(reason):无条件跳过装饰的用例,并返回跳过的原因reason。
unittest.skipIf(condition,reason):condition条件为真时,跳过装饰的用例,并返回跳过的原因reason。
unittest.skipUnless(condition,reason):condition条件为假时,跳过装饰的用例,并返回跳过的原因reason。
"""
return None
if __name__ == '__main__':
# unittest.main()#直接执行用例
Generate_reports()
生成报告是结果截图: