ScalaTest's JUnit3Suite and JUnit Test Runner

Hello,

Is it possible to run ScalaTest tests that extend JUnit3Suite in IntelliJ's JUnit test runner?

For example:

package codetest

import org.scalatest.junit.JUnit3Suite

class SimpleTest extends JUnit3Suite {

  def testSomething() {
    assert(true === true)
  }
}


When I try to create a JUnit run configuration, I get a warning message when trying to find the class to run "Class 'codetest.SimpleTest isn't test class".

Looking at JUnit3Suite, it seems designed to work with JUnit test runners:

trait JUnit3Suite extends junit.framework.TestCase with org.scalatest.Suite with scala.ScalaObject { ... }

Great work on the plugin, I'm excited to see all the new features with every release.

Cheers,
Dan

评论操作 固定链接

JUnit runner is not our feature, we just try to show for JUnit plugin that scala class is test class (should be considered as hack). I'll try your case and fix it if it really should work.

Thanks for your request,
Alexander.

0
评论操作 固定链接

Hi Alexander,

Thanks for checking into this.

I can see a similar situation for the specs framework, which wraps the test suite in a JUnit 4 compatible suite

http://code.google.com/p/specs/wiki/RunningSpecs

Run your specification with JUnit4

To execute your specification with JUnit, you need to use the JUnit4 class:

import org.specs.runner.JUnit4

class mySpecTest extends JUnit4(mySpec)
object mySpec extends Specification { ... }

The name of the test class will be <package name>.mySpecTest

Note: we should declare the JUnit runner as a class and not an object, otherwise Ant and Maven test tasks won't be able to instantiate the class properly. Moreover, following the convention of having 'Test' at the end of the name should be make it being picked up by default.

On the command line, you can run your suite by executing: java -cp ... org.junit.runner.JUnitCore mySpecTest


It looks like Eclipse has a hack to run these specs by adding the testoutput folder to the classpath

Run your specification with JUnit4 in Eclipse

In order to be able to select your JUnit4 classes, you need to add the output directory of your project to your build path:

  1. Select your project
  2. Go to "Build Path / Configure Build Path"
  3. In the "Libraries" tab, "Add Class Folder"
  4. In the "Class Folder Selection" dialog, do "Create New Folder"
  5. In the "New Folder" dialog, select "Advanced" and "Link to folder in the file system"
  6. Select the output folder of your project
  7. Refresh your project (F5 on the project's folder)

You should now be able to select the JUnit4 classes of your Scala project.

Specs already work in IntelliJ by running it as a regular application with a main() method, just not directly with the JUnit test runner.

Cheers,
Dan

0
评论操作 固定链接

Your case you can execute with IDEA JUnit runner, but it will be with cross, that it's not valid JUnit configuration (It's not good, but also is not critical, I think).
This problems can be solved by adding our ScalaTest runner (because it will be better to run such cases by ScalaTest runner not JUnit). It can be not soon.
Now you can use JUnit3 in not handy way such as:


import junit.framework.Test
import junit.framework.TestResult
import junit.framework.TestSuite

object TestClass extends TestSuite {
  addTest(new Test {
    def run(result: TestResult): Unit = {
      assert(false)
    }

    def countTestCases: Int = 1
  })

  def suite: Test = TestClass
}

0
评论操作 固定链接

alefas wrote:

Your case you can execute with IDEA JUnit runner, but it will be with cross, that it's not valid JUnit configuration (It's not good, but also is not critical, I think).
This problems can be solved by adding our ScalaTest runner (because it will be better to run such cases by ScalaTest runner not JUnit). It can be not soon.



Sorry, I didn't follow that last part.  Is there a ScalaTest runner plugin for IDEA available now, or is one planned?

Thanks!
Dan

0
评论操作 固定链接

I said that it will not soon. I meant that it's not implemented, but it's planned. Sorry for my bad english:)

0
评论操作 固定链接

That's exciting news!  For now we can get away with using JUnit or another run target to launch the ScalaTest runner.

Thanks,
Dan

0
评论操作 固定链接

Unfortunatly you was right.
It was strange bug in our plugin (unexpected behavior of .type). From next release you'll be able to use JUnit plugin with JUnit3Suite instances.
And I hope I'll finish work with ScalaTest until next release (so you'll be able to run any Suite instances).

With best regards,
Alexander Podkhalyuzin.

0
评论操作 固定链接

Today I tried to get JUnit3Suite working in IDEA.  I used the small test class from the first post:

import org.scalatest.junit.JUnit3Suite

class SimpleTest extends JUnit3Suite {
  def testSomething() {
    assert(true === true)
  } }


And from the context menu I selected Run "SimpleTest".  After the compilation IDEA opens its JUnit test runner and  shows the error message "Unable to attach test reporter to test framework or test framework quit unexpectadly", but the content in the console looks OK:

Testing started at 11:42 ...

Process finished with exit code 0


Is it a bug?

0
评论操作 固定链接

I checked your problem.
First of all you have two possible workarounds:
1. Run JUnit. not ScalaTest.
2. Run ScalaTest but on object.

Similar issue is http://youtrack.jetbrains.net/issue/SCL-3588. You can watch this issue, I try to fix it today (with your issue in mind).

Best regards,
Alexander Podkhalyuzin.

0
评论操作 固定链接

Ok, the problem should be fixed now. Please check it with next nightly or with next build from plugin manager (it will be really soon, just after fixing last Critical issue on compilation).

Best regards,
Alexander Podkhalyuzin.

0
评论操作 固定链接

Thanks for your efforts.  Today I tested it again with IDEA 110.3 and Scala plugin 0.5.47, but it still does not work.  I used the following code for my test:

import org.scalatest.junit.JUnit3Suite


class SimpleTest extends JUnit3Suite {


  def testSomething() {
    assert(true === false)
  }


}


When I right-click on the code and select "Run SimpleTest", IDEA opens the Unit test runner, but seems to fail to connect to the test.  The test runs though and I don't see any failures.  IDEA shows "Done 0 of 1 (0.81 s)" when the test is finished.
0
评论操作 固定链接

Can you send me configured project with this example (Maven based?)? Or just Scala version, ScalaTest version, JUnit version? And screenshot of running window?
What about running JUnit, not ScalaTest? Is it works? (you can create JUnit configuration manually)

Best regards,
Alexander Podkhalyuzin.

0
评论操作 固定链接

OK, maybe there is a problem with my maven configuration, because when I create a trivial Scala project with minimal dependencies my JUnit3Suite test runs fine.

Edit:
Now I found the problem.  I needed to manually include the JUnit dependency.  I was expecting org.scalatest to do so, but seemingly it doesn't.

0

请先登录再写评论。