/** * 生成测试用例table * @param TestCase $testCase * @return ReportTable */ protected function getTestCaseTable(TestCase $testCase) { //计算测试用例数据 $table = new ReportTable(); $table->setHeaders([__('Test Method'), __('Test Result'), __('Messages')]); $rows = []; foreach ($testCase->getTestCaseReport()->getTestMethodReports() as $testMethodReport) { $rows[] = [$testMethodReport->getMethod()->getName(), $testMethodReport->getTestResult() ? __('Success') : __('Failed'), implode(PHP_EOL, $testMethodReport->getMessages()) ?: 'None']; } $table->setRows($rows); return $table; }
/** * @param TestCase $testCase */ function addTestCase(TestCase $testCase) { $testCase->setTestSuite($this); $this->testCases[] = $testCase; }
/** * 执行测试用例 * @param TestCase $testCase */ protected function runTestCase(TestCase $testCase) { //给测试用例传递Mechanic $testCase->setMechanic($this); $testMethods = $this->getTestCaseTestMethods($testCase); $this->dispatcher->dispatch(EventStore::TEST_CASE_EXECUTE, new Event(EventStore::TEST_CASE_EXECUTE, $this, ['testCase' => $testCase, 'testMethods' => $testMethods])); foreach ($testMethods as $testMethod) { try { //执行用例方法,如果方法没有明确返回false,则用例方法算执行成功 $result = $testMethod->invoke($testCase); $result = $result !== false; $message = ''; } catch (\Exception $e) { $result = false; $message = $e->getMessage(); } //记录用例方法的测试报告到用例报告 $testCase->getTestCaseReport()->addTestMethodReport(TestMethodReport::create($testMethod, $result, [$message])); } $this->dispatcher->dispatch(EventStore::TEST_CASE_EXECUTED, new Event(EventStore::TEST_CASE_EXECUTED, $this, ['testCase' => $testCase])); }