What is the right way to use ScalaTest's BeforeAndAfterAll trait with sbt and IntelliJ IDEA? - intellij-idea

I'm attempting to set up a testing framework for Spark jobs. I'd like to use spark-testing-base's SharedSparkContext trait which relies on ScalaTest's BeforeAndAfterAll trait to manage setup and tear-down. Something about my current environment is causing the beforeAll and afterAll methods to be called around each test case.
(Even if I wanted to permit this redundant behavior, I couldn't: I don't know how to tear down my HiveContext object properly, so the second call to beforeAll throws an exception that bottoms out at "ERROR XSDB6: Another instance of Derby may have already booted the database /Users/applemacbookpro/git/my-project/metastore_db.")
I'm using IntelliJ IDEA with an SBT-managed build.
MacOS 10.11.4
IntelliJ IDEA 2016.1.3
not sure about SBT version, should be recent
ScalaTest 2.2.6
Per the README of spark-testing-base and this question, I've put
parallelExecution in Test := false
in build.sbt.
Here's my example:
import org.scalatest.{BeforeAndAfterAll, FlatSpec}
class ExampleSpec extends FlatSpec with BeforeAndAfterAll {
override def beforeAll(): Unit = {
println("in beforeAll")
super.beforeAll()
}
override def afterAll() {
println("in afterAll")
super.afterAll()
}
behavior of "example"
it should "succeed" in {
println("test 1")
}
it should "succeed again" in {
println("test2")
}
}
I trigger it by right-clicking in the editor window and running from the context menu; the output is:
/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/bin/java...
Testing started at 2:50 PM ...
in beforeAll
test 1
in afterAll
in beforeAll
test2
in afterAll
Process finished with exit code 0

I think it's and Intellij/Scalatest bug.
I can reproduce your case, when right-clicking in the "Editor" window.
But if you right click on your class in the "Project" window and then run from the context menu, it works as expected :
in beforeAll
test 1
test2
in afterAll
When right-clicking in the editor window, Intellij seems to instantiate 2 runners, one for each test method.
You can see in the "Run/Debug" window that ExampleSpec class appears several times instead of once.

Related

how to run only one test in scalatest/playspec

My spec file has several tests
"HomeController index page" should {
"have title Welcome " in {
....
}
"Home controller " should {
"render homepage with csrfToken" in {
...
}
To run the tests in IntelliJ, I right-click on the spec file and select run. But this runs all the tests. Is there a way to select tests and run only the selected ones?
You should be able to run individual tests by placing the cursor inside the test method and pressing Ctrl+Shift+F10 or by creating the ScalaTest run/debug configuration where you can specify the test name to run.
See also Test scopes in Scala section in the documentation:

Running a main-like in a non-main package

We have a package with a fair number of complex tests. As part of the test suite, they run on builds etc.
func TestFunc(t *testing.T) {
//lots of setup stuff and defining success conditions
result := SystemModel.Run()
}
Now, for one of these tests, I want to introduce some kind of frontend which will make it possible for me to debug a few things. It's not really a test, but a debug tool. For this, I want to just run the same test but with a Builder pattern:
func TestFuncWithFrontend(t *testing.T) {
//lots of setup stuff and defining success conditions
result := SystemModel.Run().WithHTTPFrontend(":9999")
}
The test then would only start if I send a signal via HTTP from the frontend. Basically WithHTTPFrontend() just waits with a channel on a HTTP call from the frontend.
This of course would make the automated tests fail, because no such signal will be sent and execution will hang.
I can't just rename the package to main because the package has 15 files and they are used elsewhere in the system.
Likewise I haven't found a way to run a test only on demand while excluding it from the test suite, so that TestFuncWithFrontend would only run from the commandline - I don't care if with go run or go test or whatever.
I've also thought of ExampleTestFunc() but there's so much output produced by the test it's useless, and without defining Output: ..., the Example won't run.
Unfortunately, there's also a lot of initialization code at (private, i.e. lower case) package level that the test needs. So I can't just create a sub-package main, as a lot of that stuff wouldn't be accessible.
It seems I have three choices:
Export all this initialization variables and code with upper case, so that I could be using it from a sub-main package
Duplicate the whole code.
Move the test into a sub-package main and then have a func main() for the test with Frontend and a _test.go for the normal test, which would have to import a few things from the parent package.
I'd rather like to avoid the second option...And the first is better, but isn't great either IMHO. I think I'll go for the third, but...
am I missing some other option?
You can pass a custom command line argument to go test and start the debug port based on that. Something like this:
package hello_test
import (
"flag"
"log"
"testing"
)
var debugTest bool
func init() {
flag.BoolVar(&debugTest, "debug-test", false, "Setup debugging for tests")
}
func TestHelloWorld(t *testing.T) {
if debugTest {
log.Println("Starting debug port for test...")
// Start the server here
}
log.Println("Done")
}
Then if you want to run just that specific test, go test -debug-test -run '^TestHelloWorld$' ./.
Alternatively it's also possible to set a custom environment variable that you check in the test function to change behaviour.
I finally found an acceptable option. This answer
Skip some tests with go test
brought me to the right track.
Essentially using build tags which would not be present in normal builds but which I can provide when executing manually.

CodenameOne TestRecorder How-To run test

because I think I recommend CodenameOne to be used for development I try to investigate deeper into it. I just tried out the Test Recorder which generated a test class.
Now my question: How-to use this test class? Do I have to call the test method from the existing UI code using e.g. a button to start it?
Generated code:
public class RegisterUserATest extends AbstractTest {
public boolean runTest() throws Exception {
clickButtonByName("Register");
keyPress(16);
keyPress(65);
waitFor(112);
keyPress(65);
setText("Name", "A");
keyPress(16);
keyPress(65);
waitFor(113);
keyPress(16);
waitFor(1);
keyPress(97);
setText("Email", "");
setText("Password", "A");
clickButtonByName("Register");
return true;
}
}
I think the solution is very easy but I cannot see it.
If this is on NetBeans right click the project and select "Test". On IntelliJ/IDEA it's under Codename One -> Run Tests.
Notice that the latter has a bug in it that will be fixed in the release coming tomorrow (October 7th 2016).

ScalaTest and IntelliJ - Running one Test at a time

I have a ScalaTest which extends the FlatSpec. I have many tests inside the test and I now want to have the possibility to run one test at a time. No matter what I do, I can't get IntelliJ to do it. In the Edit Configurations of the test, I can specify that it should run one test at a time by giving the name of the test. For example:
it should "test the sample multiple times" in new MyDataHelper {
...
}
where I gave the name as "test the sample multiple times", but it does not seem to take that and all I get to see is that it just prints Empty Test Suite. Any ideas how can this be done?
If using Gradle, go to Preferences > Build, Execution, Deployment > Build Tools > Gradle and in the Build and run > Run tests using: section, select IntelliJ IDEA if you haven't already.
An approach that works for me is to right-click (on Windows) within the definition of the test, and choose "Run MyTestClass..." -- or, equivalently, Ctrl-Shift-F10 with the cursor already inside the test. But it's a little delicate and your specific example may be causing your problem. Consider:
class MyTestClass extends FlatSpec with Matchers {
"bob" should "do something" in {
// ...
}
it should "do something else" in {
// ...
}
"fred" should "do something" in {
// ...
}
it should "do something else" in {
// ...
}
}
I can use the above approach to run any of the four tests individually. Your approach based on editing configurations works too. But if I delete the first test I can't run the second one individually -- the others are still fine. That's because a test that starts with it is intended to follow one that doesn't -- then the it is replaced with the appropriate string in the name of the test.
If you want to run the tests by setting up configurations, then the names of these four tests are:
bob should do something
bob should do something else
fred should do something
fred should do something else
Again, note the substitution for it -- there's no way to figure out the name of a test starting with it if it doesn't follow another test.
I'm using IntelliJ Idea 13.1.4 on Windows with Scala 2.10.4, Scala plugin 0.41.1, and ScalaTest 2.1.0. I wouldn't be surprised if this worked less well in earlier versions of Idea or the plugin.
I just realized that I'm able to run individual tests with IntelliJ 13.1.3 Community Edition. With the one that I had earlier 13.0.x, it was unfortunately not possible.

How to test Jobs in playframework?

I have:
#OnApplicationStart
public class SomeClass {
.. doJob() ...
}
How I can test it in my Unit Test that doJob() actually launched when application started?
I would argue that this is not a unit test, but an integration test.
You can test your Job, by simply calling it using the syntax new MyJob().now();, but as you are looking to test the #OnApplicationStart function, the you would be better off doing this as a Selenium test, and checking the data that you expect to be made available from the bootstrap job is present.