From 4ecb294b471542518c8e49c010805200169bf5c1 Mon Sep 17 00:00:00 2001 From: liugang Date: Thu, 18 Sep 2025 20:44:07 +0800 Subject: [PATCH] + [misc] Add unit tests for testtaskZen::buildTaskForImportUnitResult() method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../test/lib/testtaskzen.unittest.class.php | 110 ++++++++++++++++++ .../test/zen/buildtaskforimportunitresult.php | 30 +++++ 2 files changed, 140 insertions(+) create mode 100755 module/testtask/test/zen/buildtaskforimportunitresult.php diff --git a/module/testtask/test/lib/testtaskzen.unittest.class.php b/module/testtask/test/lib/testtaskzen.unittest.class.php index 4da9f1531a..d8dd61e442 100644 --- a/module/testtask/test/lib/testtaskzen.unittest.class.php +++ b/module/testtask/test/lib/testtaskzen.unittest.class.php @@ -340,4 +340,114 @@ class testtaskZenTest return array('error' => $e->getMessage()); } } + + /** + * Test buildTaskForImportUnitResult method. + * + * @param int $productID + * @access public + * @return object + */ + public function buildTaskForImportUnitResultTest($productID = 1) + { + // 根据不同的productID使用不同的测试数据 + $testData = array( + 1 => array( + 'execution' => 1, + 'build' => 1, + 'frame' => 'phpunit', + 'owner' => 'admin', + 'begin' => '2024-01-01', + 'end' => '2024-01-31', + 'name' => '单元测试导入任务', + 'pri' => 3, + 'desc' => '

从单元测试结果导入的测试任务

', + 'mailto' => 'admin,qa', + 'uid' => 'uid123' + ), + 2 => array( + 'execution' => 0, + 'build' => 2, + 'frame' => 'jest', + 'owner' => 'user1', + 'begin' => '2024-02-01', + 'end' => '2024-02-28', + 'name' => 'Jest单元测试', + 'pri' => 2, + 'desc' => '', + 'mailto' => '', + 'uid' => 'uid456' + ), + 5 => array( + 'execution' => 5, + 'build' => 3, + 'frame' => 'junit', + 'owner' => 'tester', + 'begin' => '2024-03-01', + 'end' => '2024-03-31', + 'name' => 'JUnit测试导入', + 'pri' => 1, + 'desc' => 'Java单元测试结果导入', + 'mailto' => 'qa,dev', + 'uid' => '' + ), + 0 => array( + 'execution' => 0, + 'build' => 1, + 'frame' => '', + 'owner' => '', + 'begin' => '2024-12-01', + 'end' => '2024-12-31', + 'name' => '无效产品ID测试', + 'pri' => 3, + 'desc' => '测试无效产品ID的情况', + 'mailto' => '', + 'uid' => 'invalid' + ), + 999 => array( + 'execution' => 999, + 'build' => 999, + 'frame' => 'unknown', + 'owner' => 'unknown', + 'begin' => '2025-01-01', + 'end' => '2025-01-31', + 'name' => '不存在的产品测试', + 'pri' => 4, + 'desc' => '测试不存在的产品ID', + 'mailto' => 'admin', + 'uid' => 'test999' + ) + ); + + $data = isset($testData[$productID]) ? $testData[$productID] : $testData[1]; + + // 保存原始$_POST和$_GET数据 + $originalPost = $_POST; + $originalGet = $_GET; + + // 模拟表单数据 + $_POST = $data; + $_GET = array(); + + try { + $testtaskZen = $this->testtaskZenTest->newInstance(); + $method = $this->testtaskZenTest->getMethod('buildTaskForImportUnitResult'); + $method->setAccessible(true); + + $result = $method->invoke($testtaskZen, (int)$productID); + if(dao::isError()) return dao::getError(); + + // 恢复原始数据 + $_POST = $originalPost; + $_GET = $originalGet; + + return $result; + } catch(Exception $e) { + // 恢复原始数据 + $_POST = $originalPost; + $_GET = $originalGet; + + return array('error' => $e->getMessage()); + } + } } \ No newline at end of file diff --git a/module/testtask/test/zen/buildtaskforimportunitresult.php b/module/testtask/test/zen/buildtaskforimportunitresult.php new file mode 100755 index 0000000000..a98df45d7d --- /dev/null +++ b/module/testtask/test/zen/buildtaskforimportunitresult.php @@ -0,0 +1,30 @@ +#!/usr/bin/env php +> 期望返回构建的任务对象 +- 测试步骤2:空产品ID测试 >> 期望返回构建的任务对象 +- 测试步骤3:不存在的产品ID测试 >> 期望返回构建的任务对象 +- 测试步骤4:大产品ID测试 >> 期望返回构建的任务对象 +- 测试步骤5:验证必须字段设置 >> 期望product、auto字段正确设置 + +*/ + +include dirname(__FILE__, 5) . '/test/lib/init.php'; +include dirname(__FILE__, 2) . '/lib/testtaskzen.unittest.class.php'; + +zenData('product')->loadYaml('product', false, 2)->gen(10); + +su('admin'); + +$testtaskZenTest = new testtaskZenTest(); + +r($testtaskZenTest->buildTaskForImportUnitResultTest(1)) && p('product,auto') && e('1,unit'); +r($testtaskZenTest->buildTaskForImportUnitResultTest(0)) && p('product,auto') && e('0,unit'); +r($testtaskZenTest->buildTaskForImportUnitResultTest(999)) && p('product,auto') && e('999,unit'); +r($testtaskZenTest->buildTaskForImportUnitResultTest(5)) && p('product,auto,name') && e('5,unit,JUnit测试导入'); +r($testtaskZenTest->buildTaskForImportUnitResultTest(1)) && p('product,auto') && e('1,unit'); \ No newline at end of file