With PlayN framework, how can I include unit tests in my project?

只愿长相守 提交于 2019-12-08 12:04:01

问题


Right now I've been maintaining a second sandbox project where I test concepts and work through roadblocks. But this is not very efficient as I end up having to duplicate a lot of code and am unable to maintain a durable set of regression tests for my game.

I do have a folder of unit tests within my project, but it is impossible to test components of the framework itself as they depend on platform-specific implementations that are not available to the core branch where development takes place.

I have located the PlayN framework's set of tests. Is it possible to leverage these for testing one's own project? Is it possible to include two Game classes within one project?

I see this question was raised here, but it never really got a satisfactory response.


回答1:


You cannot easily write unit tests that are run against the different backends supported by PlayN. Clearly it would be quite a challenge to automatically run your unit tests on an Android phone or iOS device. However, you can write unit tests that run against the Java backend pretty easily.

I generally structure my projects such that my core submodule has a test dependency on play n-java and then I run my unit tests using the playn-java backend. I have found this to work reasonably well, though I don't usually test much that directly interacts with PlayN because it's hard to unit test visual code. The things I unit test rarely make PlayN calls.

You can also unit test against the HTML5 backend, but it's reaaaaally slow. Look at HTMLUnit.

[edit: since I'm answering this question repeatedly, I'll add instructions here on how to configure your project to run unit tests against the Java backend]

Add playn-java to your core/pom.xml as a test dependency:

 <dependency>
   <groupId>com.googlecode.playn</groupId>
   <artifactId>playn-java</artifactId>
   <version>${playn.version}</version>
   <scope>test</scope>
 </dependency>

Add this to your core/pom.xml also:

<build>
  <plugins>
    <plugin>
      <groupId>com.googlecode.mavennatives</groupId>
      <artifactId>maven-nativedependencies-plugin</artifactId>
      <version>0.0.6</version>
      <executions>
        <execution>
          <id>unpacknatives</id>
          <phase>generate-resources</phase>
          <goals> <goal>copy</goal> </goals>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.12</version>
      <configuration>
        <argLine>-Djava.library.path=${basedir}/target/natives</argLine>
      </configuration>
    </plugin>
  </plugins>
</build>

which will properly set up LWJGL when running your unit tests.

Then add this to your unit test:

 static {
     JavaPlatform.register();
 }

Now you can access PlayN.foo() services in your unit tests and they'll even work.

You compile and run your tests from Maven like so:

mvn test

If you need to run your unit tests on a (Unix) build server, you'll need to ensure that the build server has a headless X windows installation installed along with the Mesa GL libraries.



来源:https://stackoverflow.com/questions/10467667/with-playn-framework-how-can-i-include-unit-tests-in-my-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!