Skip to main content
 首页 » 编程设计

python之使用 catkin_make 测试触发 python rostest

2025年05月04日7qq号

目标:

我想通过调用 package-root 中的 catkin_make test 在 ros 环境中通过 rostest 触发 python 单元测试。

ros的版本是hydro with python 2.7.3

什么有效:

沿着official instructions我编写的测试在使用命令 rostest jenkins_test_repro testAll.test 和相关的“启动”文件调用时运行良好,如 here 所述.

在直接执行脚本时也能正常工作...

测试输出:

/home/USER/VirtEnvPython/bin/python /home/USER/sandbox/metric_tester/src/jenkins_test_repro/scripts/TestMetricStarter.py 
[ROSUNIT] Outputting test results to /home/USER/.ros/test_results/jenkins_test_repro/rosunit-MetricStarter.xml 
test_server_feedback ... ok 
test_true ... ok 
------------------------------------------------------------- 
SUMMARY: 
 * RESULT: SUCCESS 
 * TESTS: 2 
 * ERRORS: 0 [] 
 * FAILURES: 0 [] 

测试文件:

#!/usr/bin/env python 
PKG = 'jenkins_test_repro' 
#import roslib; roslib.load_manifest(PKG)  # This line is not needed with Catkin. 
 
import sys 
import os 
import rostest 
import unittest 
import requests 
 
class TestMetricStarter(unittest.TestCase): 
 
    def test_server_feedback(self): 
        r = requests.get('http://SRV-ADR/MetricServer/default/index/MetricStarter' + os.getcwd()) 
        self.assertEqual(r.status_code, 200) 
 
    def test_true(self): 
        self.assertTrue(True) 
 
if __name__ == '__main__': 
    rostest.rosrun(PKG, 'MetricStarter', TestMetricStarter) 

“启动”-文件:

<launch> 
    <test test-name="MetricStarter" pkg="jenkins_test_repro" type="TestMetricStarter.py" /> 
</launch> 

问题:

为了通过 catkin_make test 调用测试内容,我需要在 CMakeLists.txt 中添加测试。 我想出了 3 种方法来做到这一点,对我来说,我仍然不清楚我真正需要做什么。对于所有方式,我都完成了一个干净的构建(删除构建/开发并重建 catkin-workspace)

  1. 因为 rostest 的使用以及与工作命令行执行的类比

    add_rostest(test/testAll.test) 
    

    但是测试好像没有执行:

    Running tests... 
    Test project /home/USER/sandbox/metric_tester/build 
        Start 1: _ctest_jenkins_test_repro_rostest_test_testAll.test 
    1/1 Test #1: _ctest_jenkins_test_repro_rostest_test_testAll.test ...   Passed    1.64 sec 
     
    100% tests passed, 0 tests failed out of 1 
     
    Total Test time (real) =   1.65 sec 
    catkin_make test  4.58s user 0.54s system 79% cpu 6.404 total 
    

    我也试过

    add_rostest(scripts/TestMetricStarter.py) 
    

    女巫导致跟随错误

    Running tests... 
    Test project /home/USER/sandbox/metric_tester/build 
        Start 1: _ctest_jenkins_test_repro_rostest_scripts_TestMetricStarter.py 
    1/1 Test #1: _ctest_jenkins_test_repro_rostest_scripts_TestMetricStarter.py ...***Failed    0.46 sec 
     
    0% tests passed, 1 tests failed out of 1 
     
    Total Test time (real) =   0.47 sec 
     
    The following tests FAILED: 
          1 - _ctest_jenkins_test_repro_rostest_scripts_TestMetricStarter.py (Failed) 
    Errors while running CTest 
    make: *** [test] Error 8 
    Invoking "make" failed 
    
  2. 刚试了一下,和rostest有关系,

    add_rostest_gtest(jenkins_test_repro test/testAll.test scripts/TestMetricStarter.py) 
    

    但由于输出和请求的链接器语言,似乎是 c/c++ 的东西

    CMake Error: CMake can not determine linker language for target:jenkins_test_repro 
    CMake Error: Cannot determine link language for target "jenkins_test_repro". 
    -- Generating done 
    -- Build files have been written to: /home/USER/sandbox/metric_tester/build 
    make: *** [cmake_check_build_system] Error 1 
    
  3. 在 official guide 中是运行 tests as part of make tests 的部分

    这会导致与第一种方式类似的行为

    catkin_add_nosetests(test/testAll.test) 
    

    与 add_rostest(scripts/TestMetricStarter.py) 相同

    catkin_add_nosetests(scripts/TestMetricStarter.py) 
    

    与 add_rostest(test/testAll.test)

  4. 相同

现在呢?

所以仍然存在我必须如何在 CMakeLists.txt 中添加测试的问题...对我来说,不清楚如何为用 python 编写的单元测试执行此操作。使用 c/cpp 有一些方法可以工作,但对于 python?

由于 build-farm 上的工具链,如果有一种方法可以使用 catkin_make test 触发测试,那就太好了。本题不要求绕过CMake的其他方式。

如果需要更多信息,请询问 :)。我希望有人可以帮助或提示正确的解决方案。

编辑:我在 http://answers.ros.org 上发布了同样的问题因为我认为这仍然是一个非常具体的问题。如果我得到答案,如果有人有类似问题,我会在此处发布。

请您参考如下方法:

仍然不清楚到底发生了什么变化,因为 add_rostest(test/testAll.test) 之前已经过测试(第一种方法)...

刚刚在 CMakeLists.txt 中添加了以下行:

if (CATKIN_ENABLE_TESTING) 
  add_rostest(test/testAll.test) 
endif()