views:

49

answers:

3

I'd like to have a single project pom but have my GUI tests always run when I'm invoking JUnit on Netbeans, but have them conditional (on an environment variable?) when building on the command line (usually for production build - on a headless machine, but sometimes just for build speed).

I don't mind instrumenting my JUnit tests for this, as I already have to set up my GUI test infrastructure, but how do I conditionalize my pom!

Netbeans 6.5 with Maven plugin.

Any ideas how I can accomplish this?

Ilane

A: 

The Surefire plugin can specify which tests to run.

You could specify different sets to run for different profiles, and select your profile using -P{profilename}.

Brian Agnew
Can that specify which tests, or which classes? If the former - I think it would be too many tests and make the pom unreadable, if the latter, then I would have to split my tests into two classes, the GUI-specific portion and the model, non-GUI methods portion, which I really don't want to do.
Ilane
A: 

One solution would be to use a profile (that could be indeed activated by an environment variable) and to exclude GUI tests from the Maven Surefire Plugin configuration using excludes in this profile, based on a naming convention. Something like this:

<profile>
  <id>headless</id>
  <activation>
    <property>
      <name>env.FOO</name>
      <value>foo</value>
    </property>
  </activation>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <excludes>
            <exclude>**/*$*</exclude><!-- to exclude inner classes -->
            <exclude>**/gui/**</exclude>
          </excludes>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
</profile>

Another option would be to use TestNG and to leverage the excludeGroups parameter.

Pascal Thivent
Yes, I think this could work for me, but with a little different twist - defining an environment variable for the surefire plugin in each profile: <environmentVariables> <GUI_TEST>true</GUI_TEST> </environmentVariables>I'm not quite sure how to get NetBeans to select a profile (I suppose I can make that the default, of course). I'll assume Brian's suggestion of using the '-P' option works on the command line. (BTW, if anyone can point me to an editing how-to for code snippets, I would be happy to read it!)
Ilane
Why not use excludes? Our general setup is to have one JUnit class per project class. For a GUI widget, that class can have both GUI and non-GUI test methods. I always want to run the non-GUI methods. Unless I misunderstand you and Brian, the excludes works per class, not per method. Unless Netbeans is creating inner classes to implement the JUnit functionality, no inner classes are really involved to exclude.Many thanks to you and Brian!
Ilane
@Ilane: I confirm, excludes works per class (and I'm not aware of a way to exclude methods with JUnit) so you would have to split your tests (or use TestNG and groups). Regarding profiles, it seems that NetBeans does allow to [declare them](http://wiki.netbeans.org/MavenBestPractices#Binding_Maven_goals_to_IDE_actions). And the exclusion of inner classes is the default rule (as [documented](http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#excludes) so I added it to not change the default behavior. Not sure it's required though, didn't check the source code.
Pascal Thivent
A: 
Ilane