Reputation: 87
I have a project that mainly uses Java. We implemented some Python functions in a package and would like to unit test them. There is a package called src/python where these .py files are located.
I have to problems implementing the tests:
Appreciate your help!
Upvotes: 0
Views: 999
Reputation: 1796
The maven-exec-plugin provides the exec goal, which can run any executable during a Maven build phase.
By configuring the plugin to run in the test phase of Maven's default lifecycle, the Python packages could be installed and tests could be executed using plugin executions.
Regarding your questions:
Maven does not provide its own test framework. It provides a lifecycle phase called "test". When building a JAR, Maven runs a default plugin, which integrates with Java test frameworks like JUnit (found on the test classpath). Additionally you can configure custom plugin executions that run also in the "test" phase, but do something with python.
Python packages could be installed the same way as you would do without Maven. The exec goal can be used to invoke your Python package manager executable.
Assumptions:
In your project's pom.xml, add following plugin configuration:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>pip-install</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<workingDirectory>src/python</workingDirectory>
<executable>pip</executable>
<arguments>
<argument>install</argument>
<argument>-r</argument>
<argument>requirements.txt</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>python-test</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<workingDirectory>src/python</workingDirectory>
<executable>python</executable>
<arguments>
<argument>-m</argument>
<argument>unittest</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
If the assumptions are incorrect, just change the executable or the arguments of the "pip-install" and/or "python-test" plugin execution.
Upvotes: 2