how to run only one test in scalatest/playspec - intellij-idea

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:

Related

Run an individual test parameter in junit 5

Lets say I have a parameterized test like this:
#ParameterizedTest
#ValueSource(strings = {"a", "B", "r"})
void test1(String val) {
assertNotNull(val);
}
What I'd like to do is just run the test for one value. Is there a way to do it? I know how to run an individual test, but if there's a notation to run just the one parameter, that would be of help. Looking to run it through Maven.
If you use Eclipse, you can specify the "uniqueId" parameter as a test run parameter.
You can extract the id (which is a very complicated string) as follows.
Run the test as usual, so Eclipse will show each single run in the JUnit panel as a tree.
Select a single run and execute it in debug mode, place a breakpoint whereever you want.
Click on the Properties menu in the Debug perspective:
Copy the uniqueId parameter from the Command Line window:
And add it as a command line parameter in your JUnit Run configuration:

How to run an specific test case in the selected environment in SoapUI

I have multiple Environment and a lot of test cases, but not all test cases are needed to be run in all environment. Is there a way to run only an specific test cases from a test suite based on the selected Environment.
For Example
If I select Environment1, it will run the following test cases
TC0001
TC0002
TC0003
TC0004
TC0005
If I select Environment2, it will run only the following test cases
TC0001
TC0003
TC0005
There can be different solution to achieve this since you have multiple environments i.e., pro software being used.
I would achieve the solution using Test Suite's Setup Script:
Create Test Suite level custom property. Use the same name as your environment name. For instance, DEV is the environment defined, use the same as test suite property name and provide the list of values separated by comma as value for that property, say TC1, TC2 etc.,
Similarly defined other environments and its values as well.
Copy the below script in Setup Script for the test suite and execute the script which enables or disables the test cases according to the environment and property value
Test Suite's Setup Script
/**
* This is soapui's Setup Script
* which enables / disables required
* test cases based on the user list
* for that specific environment
**/
def disableTestCase(testCaze) {
testCaze.disabled = true
}
def enableTestCase(testCaze) {
testCaze.disabled = false
}
def getEnvironmentSpecificList(def testSuite) {
def currentEnv = testSuite.project.activeEnvironment.NAME
def enableList = testSuite.getPropertyValue(currentEnv).split(',').collect { it.trim()}
log.info "List of test for enable: ${enableList}"
enableList
}
def userList = getEnvironmentSpecificList(testSuite)
testSuite.testCaseList.each { kase ->
if (userList.contains(kase.name)) {
enableTestCase(kase)
} else {
disableTestCase(kase)
}
}
Other way to achieve this is using Event feature of ReadyAPI, you may use TestRunListener.beforeRun() and filter the test case whether to execute or ignore.
EDIT:
If you are using ReadyAPI, then you can the new feature called tag the test cases. A test case can be tagged with multiple values and you can execute tests using specific tags. In this case, you may not needed to have the setup script as that is for the open source edition. Refer documentation for more details.
This solution is only specific to Pro software and Open Source edition does have this tag feature.

What is the right way to use ScalaTest's BeforeAndAfterAll trait with sbt and 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.

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.

Running a GEB test using Intellij

Being a beginner in GEB testing, I am trying to run a simple login program in Intellij. Could you please help me run this test in Intellij? My question is what selections should I make in the edit configurations page? Please help. This example is from the book of geb.
import geb.Browser
Browser.drive {
go "http://google.com/ncr"
// make sure we actually got to the page
assert title == "Google"
// enter wikipedia into the search field
$("input", name: "q").value("wikipedia")
// wait for the change to results page to happen
// (google updates the page dynamically without a new request)
waitFor { title.endsWith("Google Search") }
// is the first link to wikipedia?
def firstLink = $("li.g", 0).find("a.l")
assert firstLink.text() == "Wikipedia"
// click the link
firstLink.click()
// wait for Google's javascript to redirect to Wikipedia
waitFor { title == "Wikipedia" }
}
If you are running this in IntelliJ you should be able to run this as a JUnit test (ctrl+F10). Make sure that this is inside of a Class and in a method.
For ease of syntax, it would be good to use Spock as your BDD framework (include the library in your project; if using Maven, follow the guide on the site but update to Spock 0.7-groovy-2.0 and Geb 0.9.0-RC-1 for the latest libraries
If you want to switch from straight JUnit to Spock (keep in mind you should use JUnit as a silent library) then your test case looks like this:
def "show off the awesomeness of google"() {
given:
go "http://google.com/ncr"
expect: "make sure we actually got to the page"
title == "Google"
when: "enter wikipedia into the search field"
$("input", name: "q").value("wikipedia")
then: "wait for the change to results page to happen and (google updates the page dynamically without a new request)"
waitFor { title.endsWith("Google Search") }
// is the first link to wikipedia?
def firstLink = $("li.g", 0).find("a.l")
and:
firstLink.text() == "Wikipedia"
when: "click the link"
firstLink.click()
then: "wait for Google's javascript to redirect to Wikipedia"
waitFor { title == "Wikipedia" }
}
Just remember: Ctrl + F10 (best key shorcut for a test in IntelliJ!)
The above is close but no cigar, so to speak.
If you want to run bulk standard Gebish test from WITHIN Intellij,
I tried 2 things.
I added my geckodriver.exe to the test/resources under my Spock/Geb tests
I literally in the given: part of my Spok/Geb test did the following:
given:
System.setProperty("webdriver.gecko.driver", "C:\\repo\\geb-example-gradle\\src\\test\\resources" + "\\geckodriver.exe");
Failed
Succeeded
Now the usual deal with answers is, that someone writes something, you try it and then it fails.
So, if it did not work for you, use the reference Geb/Spock project on Github as follows and import it into intellij (remember, New Project, then find the gradle.build script and then intellij will import it nicely)...it also kicks off a build so dont freak out:
https://github.com/geb/geb-example-gradle
Download the driver:
https://github.com/mozilla/geckodriver/releases
and move it to the test/resource folder of the reference project you just imported under test/groovy...(see image)
Now add the above given: clause to the GebishOrgSpec Sock/Geb test:
The test runs nicely from WITHIN Intellij. Evidence the browser open and the test running:
LOVELY JOBBLY :=)