python与maven(单元测试集成)

16 投票
4 回答
16081 浏览
提问于 2025-04-16 11:07

我有一个主要使用Java的项目(用maven作为构建工具),我们想要添加一些Python模块。把代码放在src/main/python里其实挺简单的,但我还没找到一个好的方法来把maven的测试框架和我们的Python单元测试结合起来。有没有什么好的办法,让我的Java单元测试和Python单元测试可以用同一个maven目标一起运行?(如果Python单元测试的结果能出现在网站报告里,那就更好了)。

4 个回答

0

ShiningPanda的团队似乎在这方面做了一些工作,他们的博客文章解释了他们是怎么做到的,链接在这里:http://www.shiningpanda.com/blog/2012/04/25/python-java-unified-build-process-14/

1

受到这个链接的启发(正如@S.Lott提到的),这里有一个可以运行的例子:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <id>python-test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>python3</executable>
                        <workingDirectory>${project.build.directory}</workingDirectory>
                        <arguments>
                            <argument>-m</argument>
                            <argument>unittest</argument>
                            <argument>discover</argument>
                            <argument>.</argument>
                        </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>

在这个例子中,我们的测试文件放在${project.build.directory}这个地方,而构建机器上已经安装了python3

5

这看起来正是你想要的内容。还有一些其他地方也讨论了这个话题。

http://steveberczuk.blogspot.com/2009/12/continuous-integration-of-python-code.html

http://www.mojohaus.org/exec-maven-plugin/

撰写回答