Skip to content
Advertisement

How to run Python unit test with maven in Java project?

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:

  1. How can I make sure that these unit tests integrate with the maven test framework? So maven will automatically run them?
  2. How can I get maven to install the needed external python libraries that are not standard ?

Appreciate your help!

Advertisement

Answer

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:

  1. 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.

  2. 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:

  • pip is used as Python package manager
  • “pip” is available in your PATH environment variable
  • requirements.txt is located in src/python directory
  • Python tests are started with “python -m unittest”
  • “python” is available in your PATH environment variable

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.

User contributions licensed under: CC BY-SA
11 People found this is helpful
Advertisement