【问题标题】:run python script before java maven test在java maven测试之前运行python脚本
【发布时间】:2016-03-10 14:00:43
【问题描述】:

我正在尝试运行将生成用于测试的资源的 python 脚本。 python 脚本应该在任何单元测试之前运行。

注意:这仅用于测试生产中不需要所述资源。

我环顾四周,似乎无法让它工作。这就是我的 pom.xml 中的内容

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <configuration>
    <executable>python</executable>
    <workingDirectory>src/test/resources/</workingDirectory>
  </configuration>
  <executions>
    <execution>
      <id>python-setup</id>
      <phase>test</phase>
      <goals>
        <goal>exec</goal>
      </goals>
      <configuration>
        <arguments>
          <argument>testSetup.py</argument>
        </arguments>
      </configuration>
    </execution>
  </executions>
</plugin>

谁能指出这个设置有什么问题,或者做同样事情的替代方法。

我在 Mac 上运行,我正在通过运行 mvn test 进行测试。

下面是我运行 mvn test 时的输出(在 maven clean 之后)。

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building app.io 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 21 source files to /Users/steve/app.io/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 7 source files to /Users/steve/app.io/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ app ---
[INFO] Surefire report directory: /Users/steve/app.io/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

看起来exec-maven-plugin 没有被调用。

带有-X 标志的exec-maven-plugin 的输出

[DEBUG] -----------------------------------------------------------------------
[DEBUG] Goal:          org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (python-setup)
[DEBUG] Style:         Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <arguments>
    <argument>testSetup.py</argument>
  </arguments>
  <basedir default-value="${basedir}"/>
  <classpathScope default-value="runtime">${exec.classpathScope}</classpathScope>
  <commandlineArgs>${exec.args}</commandlineArgs>
  <executable>python</executable>
  <failWithEmptyArgument default-value="true"/>
  <failWithNullKeyOrValueInEnvironment default-value="true"/>
  <longClasspath default-value="false">${exec.longClasspath}</longClasspath>
  <outputFile>${exec.outputFile}</outputFile>
  <project default-value="${project}"/>
  <skip default-value="false">${skip}</skip>
  <sourceRoot>${sourceRoot}</sourceRoot>
  <testSourceRoot>${testSourceRoot}</testSourceRoot>
  <workingDirectory>src/test/resources/</workingDirectory>
  <session default-value="${session}"/>
</configuration>
[DEBUG] =======================================================================
[DEBUG] Dependency collection stats: {ConflictMarker.analyzeTime=2, ConflictMarker.markTime=0, ConflictMarker.nodeCount=239, ConflictIdSorter.graphTime=1, ConflictIdSorter.topsortTime=0, ConflictIdSorter.conflictIdCount=97, ConflictIdSorter.conflictIdCycleCount=0, ConflictResolver.totalTime=13, ConflictResolver.conflictItemCount=180, DefaultDependencyCollector.collectTime=401, DefaultDependencyCollector.transformTime=20}
[DEBUG] com.elderstudios:app:jar:0.0.1-SNAPSHOT
[DEBUG]    org.springframework.boot:spring-boot-starter-web:jar:1.3.2.RELEASE:compile
[DEBUG]       org.springframework.boot:spring-boot-starter:jar:1.3.2.RELEASE:compile
[DEBUG]          org.springframework.boot:spring-boot:jar:1.3.2.RELEASE:compile
[DEBUG]          org.springframework.boot:spring-boot-autoconfigure:jar:1.3.2.RELEASE:compile
[DEBUG]          org.springframework.boot:spring-boot-starter-logging:jar:1.3.2.RELEASE:compile
[DEBUG]             ch.qos.logback:logback-classic:jar:1.1.3:compile
[DEBUG]                ch.qos.logback:logback-core:jar:1.1.3:compile
[DEBUG]             org.slf4j:jul-to-slf4j:jar:1.7.13:compile
[DEBUG]             org.slf4j:log4j-over-slf4j:jar:1.7.13:compile
[DEBUG]          org.yaml:snakeyaml:jar:1.16:runtime
[DEBUG]       org.springframework.boot:spring-boot-starter-tomcat:jar:1.3.2.RELEASE:compile
[DEBUG]          org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.30:compile
[DEBUG]          org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.30:compile
[DEBUG]          org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.30:compile
[DEBUG]          org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.30:compile
[DEBUG]       org.springframework.boot:spring-boot-starter-validation:jar:1.3.2.RELEASE:compile
[DEBUG]          org.hibernate:hibernate-validator:jar:5.2.2.Final:compile
[DEBUG]             javax.validation:validation-api:jar:1.1.0.Final:compile
[DEBUG]             com.fasterxml:classmate:jar:1.1.0:compile
[DEBUG]       com.fasterxml.jackson.core:jackson-databind:jar:2.6.5:compile
[DEBUG]          com.fasterxml.jackson.core:jackson-annotations:jar:2.6.5:compile (version managed from 2.6.0 by org.springframework.boot:spring-boot-dependencies:1.3.2.RELEASE)
[DEBUG]          com.fasterxml.jackson.core:jackson-core:jar:2.6.5:compile
[DEBUG]       org.springframework:spring-web:jar:4.2.4.RELEASE:compile
[DEBUG]       org.springframework:spring-webmvc:jar:4.2.4.RELEASE:compile
[DEBUG]          org.springframework:spring-expression:jar:4.2.4.RELEASE:compile
[DEBUG]    org.springframework.boot:spring-boot-starter-test:jar:1.3.2.RELEASE:test
[DEBUG]       org.mockito:mockito-core:jar:1.10.19:test
[DEBUG]          org.objenesis:objenesis:jar:2.1:test
[DEBUG]       org.hamcrest:hamcrest-core:jar:1.3:test
[DEBUG]       org.hamcrest:hamcrest-library:jar:1.3:test
[DEBUG]       org.springframework:spring-core:jar:4.2.4.RELEASE:compile
[DEBUG]       org.springframework:spring-test:jar:4.2.4.RELEASE:test
[DEBUG]    org.springframework.boot:spring-boot-starter-data-jpa:jar:1.3.2.RELEASE:compile
[DEBUG]       org.springframework.boot:spring-boot-starter-aop:jar:1.3.2.RELEASE:compile
[DEBUG]          org.aspectj:aspectjweaver:jar:1.8.8:compile
[DEBUG]       org.springframework.boot:spring-boot-starter-jdbc:jar:1.3.2.RELEASE:compile
[DEBUG]          org.apache.tomcat:tomcat-jdbc:jar:8.0.30:compile
[DEBUG]             org.apache.tomcat:tomcat-juli:jar:8.0.30:compile
[DEBUG]          org.springframework:spring-jdbc:jar:4.2.4.RELEASE:compile
[DEBUG]       org.hibernate:hibernate-entitymanager:jar:4.3.11.Final:compile
[DEBUG]          org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile (version managed from 3.1.3.GA by org.springframework.boot:spring-boot-dependencies:1.3.2.RELEASE)
[DEBUG]          org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[DEBUG]          org.hibernate:hibernate-core:jar:4.3.11.Final:compile
[DEBUG]             antlr:antlr:jar:2.7.7:compile
[DEBUG]             org.jboss:jandex:jar:1.1.0.Final:compile
[DEBUG]          dom4j:dom4j:jar:1.6.1:compile
[DEBUG]             xml-apis:xml-apis:jar:1.0.b2:compile
[DEBUG]          org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[DEBUG]          org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[DEBUG]          org.javassist:javassist:jar:3.18.1-GA:compile
[DEBUG]       javax.transaction:javax.transaction-api:jar:1.2:compile
[DEBUG]       org.springframework.data:spring-data-jpa:jar:1.9.2.RELEASE:compile
[DEBUG]          org.springframework.data:spring-data-commons:jar:1.11.2.RELEASE:compile
[DEBUG]          org.springframework:spring-orm:jar:4.2.4.RELEASE:compile (version managed from 4.1.9.RELEASE by org.springframework:spring-framework-bom:4.2.4.RELEASE)
[DEBUG]          org.springframework:spring-tx:jar:4.2.4.RELEASE:compile (version managed from 4.1.9.RELEASE by org.springframework:spring-framework-bom:4.2.4.RELEASE)
[DEBUG]          org.slf4j:jcl-over-slf4j:jar:1.7.13:compile
[DEBUG]       org.springframework:spring-aspects:jar:4.2.4.RELEASE:compile
[DEBUG]    org.springframework.hateoas:spring-hateoas:jar:0.19.0.RELEASE:compile
[DEBUG]       org.springframework:spring-aop:jar:4.2.4.RELEASE:compile (version managed from 4.1.7.RELEASE by org.springframework:spring-framework-bom:4.2.4.RELEASE)
[DEBUG]          aopalliance:aopalliance:jar:1.0:compile
[DEBUG]       org.springframework:spring-beans:jar:4.2.4.RELEASE:compile (version managed from 4.1.7.RELEASE by org.springframework:spring-framework-bom:4.2.4.RELEASE)
[DEBUG]       org.springframework:spring-context:jar:4.2.4.RELEASE:compile (version managed from 4.1.7.RELEASE by org.springframework:spring-framework-bom:4.2.4.RELEASE)
[DEBUG]       org.slf4j:slf4j-api:jar:1.7.13:compile (version managed from 1.7.12 by org.springframework.boot:spring-boot-dependencies:1.3.2.RELEASE)
[DEBUG]    mysql:mysql-connector-java:jar:5.1.38:compile
[DEBUG]    org.json:json:jar:20140107:compile
[DEBUG]    com.google.code.gson:gson:jar:2.3.1:compile
[DEBUG]    javax.interceptor:javax.interceptor-api:jar:1.2:compile
[DEBUG]    com.google.api-client:google-api-client:jar:1.20.0:compile
[DEBUG]       com.google.oauth-client:google-oauth-client:jar:1.20.0:compile
[DEBUG]       com.google.http-client:google-http-client-jackson2:jar:1.20.0:compile
[DEBUG]       com.google.guava:guava-jdk5:jar:13.0:compile
[DEBUG]    com.google.http-client:google-http-client:jar:1.19.0:compile
[DEBUG]       com.google.code.findbugs:jsr305:jar:1.3.9:compile
[DEBUG]       org.apache.httpcomponents:httpclient:jar:4.5.1:compile (version managed from 4.0.1 by org.springframework.boot:spring-boot-dependencies:1.3.2.RELEASE)
[DEBUG]          org.apache.httpcomponents:httpcore:jar:4.4.4:compile (version managed from 4.4.3 by org.springframework.boot:spring-boot-dependencies:1.3.2.RELEASE)
[DEBUG]          commons-codec:commons-codec:jar:1.9:compile
[DEBUG]    com.google.http-client:google-http-client-gson:jar:1.21.0:compile
[DEBUG]    junit:junit:jar:4.12:test
[DEBUG]    org.assertj:assertj-core:jar:3.2.0:test
[DEBUG]    com.github.springtestdbunit:spring-test-dbunit:jar:1.2.1:test
[DEBUG]    org.dbunit:dbunit:jar:2.5.1:test
[DEBUG]       commons-collections:commons-collections:jar:3.2.2:test (version managed from 3.2.1 by org.springframework.boot:spring-boot-dependencies:1.3.2.RELEASE)
[DEBUG]       org.apache.poi:poi-ooxml:jar:3.11:test
[DEBUG]          org.apache.poi:poi:jar:3.11:test
[DEBUG]          org.apache.poi:poi-ooxml-schemas:jar:3.11:test
[DEBUG]             org.apache.xmlbeans:xmlbeans:jar:2.6.0:test
[DEBUG]                stax:stax-api:jar:1.0.1:test
[DEBUG]    net.javacrumbs.json-unit:json-unit:jar:1.9.0:test
[DEBUG]       net.javacrumbs.json-unit:json-unit-core:jar:1.9.0:test
[DEBUG]    com.jayway.jsonpath:json-path:jar:2.0.0:test
[DEBUG]       net.minidev:json-smart:jar:2.1.1:test
[DEBUG]          net.minidev:asm:jar:1.0.2:test
[DEBUG]             asm:asm:jar:3.3.1:test
[DEBUG]    org.hamcrest:hamcrest-all:jar:1.3:test
[DEBUG]    org.springframework.restdocs:spring-restdocs-mockmvc:jar:1.0.1.RELEASE:test
[DEBUG]       org.springframework.restdocs:spring-restdocs-core:jar:1.0.1.RELEASE:test
[DEBUG]          javax.servlet:javax.servlet-api:jar:3.1.0:test
[INFO] 

更新

我在乱七八糟地试图偶然发现答案,发现如果我将 maven-surefire-plugin 更改为 不包括任何测试,脚本会按预期执行和工作。当我说不包含测试时,没有以 ...Tests.java 结尾的测试文件

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <includes>
            <include>**/*Tests.java</include>  <!-- there is no *Tests.java, it was *Test.java before -->
        </includes>
    </configuration>
</plugin>

【问题讨论】:

  • 你能发布你收到的错误信息吗?
  • 我没有收到错误消息,测试按预期运行,但似乎没有执行 python 脚本
  • 你的路径中有 python 吗?另外,您可以通过命令行直接执行目标并验证日志吗?没有任何 maven 日志就很难调试 :) 因为 maven exec 插件绝对是这样做的正确方法
  • 是的,python 在我的路径中,我自己运行得很好。看起来exec-maven-plugin 没有被调用。我将在上面发布输出
  • 我有点困惑,因为测试阶段是有效阶段。我认为您提供的 sn-p 没有任何问题。尝试使用 -X 执行它以启用更多登录

标签: java python maven testing junit


【解决方案1】:

我认为问题在于您的 maven-exec-plugin 在“测试”阶段被调用。所以它不知道应该先运行什么 - surefire 或 exec。

尝试将执行移至不同的阶段(我认为最好的阶段是“流程测试资源”):

<executions>
  <execution>
    <id>python-setup</id>
    <phase>process-test-resources</phase>
     <goals>
     <goal>exec</goal>
    </goals>
    <configuration>
      <arguments>
        <argument>testSetup.py</argument>
      </arguments>
    </configuration>
  </execution>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-22
    • 2014-01-12
    • 2013-03-20
    • 2015-06-01
    • 2017-04-27
    • 2019-04-28
    • 1970-01-01
    • 2015-11-18
    相关资源
    最近更新 更多