How to run single test in grails-core project using Gradle? - unit-testing

I'm trying to add a new test case to existing test org.codehaus.groovy.grails.web.mapping.ReverseUrlMappingTests in grails-test-suite-web submodule of grails-core project.
https://github.com/grails/grails-core/blob/master/grails-test-suite-web/src/test/groovy/org/grails/web/mapping/ReverseUrlMappingTests.groovy
I have a problem with running single test case using Gradle. When I do:
./gradlew -Dtest.single=ReverseUrlMappingTests :grails-test-suite-web:test
It ends with:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> Could not find matching test for pattern: ReverseUrlMappingTests
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
This test can be run in full test cycle ./gradlew test but it's waste of the time when we wanna red-green-refactor only one simple test case. I also noticed that the same problem occurs when I try to run in the same manner spock.lang.Specification subclass test.
Here's my question: is it possible to run single grails-core test with Gradle?
It's grails-core 2.4.x branch clone

The question has been edited to indicate that the test is on the 2.4.x branch, which affects the answer. I have made relevant changes below.
Something like this will work...
./gradlew :g-t-s-w:test --tests *ReverseUrlMappingTests*
When you do that, you are still going to be running some other tests because of the way our build is written. If you want to get rid of them you can comment out the following:
test.dependsOn execIsolatedTests
That is in grails-test-suite-web/build.gradle at https://github.com/grails/grails-core/blob/644233cfff266f391c44ef3ec56036a3b1c9bb19/grails-test-suite-web/build.gradle#L98
You could also do something like this...
./gradlew -DsingleTest.single=ReverseUrlMappingTests grails-test-suite-web:singleTest

Related

Vue CLI plugin to add Jest test runner doesn't transpile .js files

Just a quick disclaimer, I cannot reproduce this on another project, which is why I'm here instead of making an issue on the Github.
Issue
So a quick demo project I made to show a co-worker how to use the Vue test utils, went off the wagon. It doesn't get picked up in the transform part of the jest.config.js.
I made the project with the Vue CLI, using vue create demo-project choosing ESlint and Babel, then running vue add #vue/cli-plugin-unit-jest. Everything went fine, I committed, ran the test command and everything went fine.
So we started testing snapshots, and everything was fine. Next day, come back to the project to keep going, and this is the error I get.
> vue-jest#0.1.0 test:unit .../demo-projects/vue-jest
> vue-cli-service test:unit
FAIL tests/unit/example.spec.js
● Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
.../demo-projects/vue-jest/tests/unit/example.spec.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { shallowMount } from '#vue/test-utils';
^
SyntaxError: Unexpected token {
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.163s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
Just running the normal npm run test:unit results in the example.spec.js that comes with the test utils is not babel'ed, and fails in the Node test environment.
Attempted Solutions
Did not work
Deleting node_modules and reinstalling
Deleting package-lock.json, in addition to the above step ( separately done )
Reverting back to the commit when it was working
Checking out to a different branch, deleting all the files that vue add #vue/cli-plugin-unit-jest added, then running the command again to re-create all the files
jest.config.js
tests/ - and all it's files
Worked
Pretty clear what's happening, so in the jest.config.js file, I added the .js file under the transform field.
transform: {
'^.+\\.vue$': 'vue-jest',
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
'^.+\\.(js|jsx)?$': 'babel-jest' // added .js
},
This solution solves the problem, and properly transforms the example.spec.js
Attempted Recreation
I tried to see if this was a bug by creating another project, running the same steps and it worked just fine. I even copied and pasted all the files from the broken one to the new one, looking for differences in git. It was exactly the same. Which is why I thought it might be node_modules, but it still didn't work.
Uhh
Hopefully this isn't a waste of time for anyone reading since it's not truly a problem. I mainly wanted to have it for anyone who might have the same issue to see that adding the .js extension would solve the problem without having to start over, but also curious if anyone has insight on why this might be happening?

How to run nosetests in jenkins

I have a project which has two folders on same level
/home/ishan/my_repo/jenkins_test/business_logic
/home/ishan/my_repo/jenkins_test/test_cases
test_cases has two files test_fib and test_fact
when I run nosetests --exe at /home/ishan/my_repo/jenkins_test/ it runs correctly showing
....
----------------------------------------------------------------------
Ran 4 tests in 0.036s
OK
I am trying to run these test cases so, I created following script at /home/ishan/my_repo
#!/bin/bash
source /home/ishan/venv/bin/activate
nosetests --exe /home/ishan/sf_shared/my_repo/jenkin_test/
deactivate
When I run it using
source /home/ishan/my_repo/test_runner.sh it shows correct output.
So, I tried to put it in jenksins build step. So, I added the same line
source /home/ishan/my_repo/test_runner.sh in command section of Execute Shell in jenkins.
Now, when I trigger the build using build now it fails saying
Started by user anonymous
Building in workspace /var/lib/jenkins/workspace/jenkins_test
[jenkins_test] $ /bin/sh -xe /tmp/hudson5020664150857393715.sh
+ source /home/ishan/sf_shared/test_runner.sh
/tmp/hudson5020664150857393715.sh: 2: /tmp/hudson5020664150857393715.sh: source: not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
I think it doesn't even execute any test cases. It fails long before.
Can you suggest what am I doing wrong?
Maybe this will works:
/home/ishan/venv/bin/nosetests --exe /home/ishan/sf_shared/my_repo/jenkin_test/
Resolved it, issue was with the following line
source /home/ishan/venv/bin/activate
I replaced it source with standard . then it worked. So, my line became
. /home/ishan/venv/bin/activate

launch one jasmine test with grunt and karma?

I have a big web project with many javascript Jasmine unit tests.
In that web project, i use grunt, karma and jasmine.
Is there any way to launch only one test javascript with grunt with a command line ?
Launching all tests is long, so how to do that without modify my gruntfile.js or my karma-unit.conf.js ??
I tried the following command, but it is more a hack than a real solution :
karma run -- --grep=filteredtestexpr
I am not sure of the best way to specify which test to run from the command line. But it seems like your problem can be solved with the following two pointers:
Change the name of a test from it to iit and karma will run only that test (actually all iit tests).
Change describe to ddescribe to run the entire describe block.
Also, use xit and xdescribe to explicitly exclude tests.

How to run only one unit test class using Gradle

I am new to Gradle. I use Gradle 1.10 and Ubuntu 13.
I want to know if there's any command to execute only one unit test class, similar to testOnly in SBT.
To run a single test class Airborn's answer is good.
With using some command line options, which found here, you can simply do something like this.
gradle test --tests org.gradle.SomeTest.someSpecificFeature
gradle test --tests '*SomeTest.someSpecificFeature'
gradle test --tests '*SomeSpecificTest'
gradle test --tests 'all.in.specific.package*'
gradle test --tests '*IntegTest'
gradle test --tests '*IntegTest*ui*'
gradle test --tests '*IntegTest.singleMethod'
gradle someTestTask --tests '*UiTest' someOtherTestTask --tests '*WebTest*ui'
From version 1.10 of gradle it supports selecting tests, using a test filter. For example,
apply plugin: 'java'
test {
filter {
//specific test method
includeTestsMatching "org.gradle.SomeTest.someSpecificFeature"
//specific test method, use wildcard for packages
includeTestsMatching "*SomeTest.someSpecificFeature"
//specific test class
includeTestsMatching "org.gradle.SomeTest"
//specific test class, wildcard for packages
includeTestsMatching "*.SomeTest"
//all classes in package, recursively
includeTestsMatching "com.gradle.tooling.*"
//all integration tests, by naming convention
includeTestsMatching "*IntegTest"
//only ui tests from integration tests, by some naming convention
includeTestsMatching "*IntegTest*ui"
}
}
For multi-flavor environments (a common use-case for Android), check this answer, as the --tests argument will be unsupported and you'll get an error.
In versions of Gradle prior to 5, the test.single system property can be used to specify a single test.
You can do gradle -Dtest.single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest.single=ClassName*Test test you can find more examples of filtering classes for tests under this link.
Gradle 5 removed this option, as it was superseded by test filtering using the --tests command line option.
In case you have a multi-module project :
let us say your module structure is
root-module
-> a-module
-> b-module
and the test(testToRun) you are looking to run is in b-module, with full path : com.xyz.b.module.TestClass.testToRun
As here you are interested to run the test in b-module, so you should see the tasks available for b-module.
./gradlew :b-module:tasks
The above command will list all tasks in b-module with description.
And in ideal case, you will have a task named test to run the unit tests in that module.
./gradlew :b-module:test
Now, you have reached the point for running all the tests in b-module, finally you can pass a parameter to the above task to run tests which matches the certain path pattern
./gradlew :b-module:test --tests "com.xyz.b.module.TestClass.testToRun"
Now, instead of this if you run
./gradlew test --tests "com.xyz.b.module.TestClass.testToRun"
It will run the test task for both module a and b, which might result in failure as there is nothing matching the above pattern in a-module.
Please note that --tests option may not work if you have different build types/flavors (fails with Unknown command-line option '--tests'). In this case, it's necessary to specify the particular test task (e.g. testProdReleaseUnitTest instead of just test)
After much figuring out, the following worked for me:
gradle test --tests "a.b.c.MyTestFile.mySingleTest"
For multi modules projects it's necessary to use module's name and buildType:
./gradlew :module_name:testDebugUnitTest --tests com.package_name.TestsClass.*
To run some test method the same command, but with test's name:
./gradlew :module_name:testDebugUnitTest --tests com.package_name.TestsClass.test
Below is the command to run a single test class using gradlew command line option:
gradlew.bat Connected**your bundleVariant**AndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.TestClass
Below example to run class com.example.TestClass with variant Variant_1:
gradlew.bat ConnectedVariant_1AndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.TestClass
Run a single test called MyTest:
./gradlew app:testDebug --tests=com.example.MyTest
You should try to add asteriks (*) to the end.
gradle test --tests "com.a.b.c.*"
In my case, my eclipse java compiler warnings were set too high, and eclipse was not recognizing my class as valid for execution. Updating my compiler settings fixed the problem. You can read more about it here: annotation-nonnull-cannot-be-resolved
This worked for me
Release case:
gradle testReleaseUnitTest --tests testClass
Debug case:
gradle testDebugUnitTest --tests AsyncExecutorTest
You can see de projects with: gradle -q projects and move to the project where is the class to test

Is it possible to run a single test out of Scala's test suite?

It is possible to run Scala's partest test suite with something like ant test, but is it possible to just rerun a single, failed test?
Additionally, is it possible to run only tests which have failed in a test run before?
If you are referring to the Scala repository build and the partest test suite, after having run the default ant build task, you can either run:
test/partest --failed
to run the last set of tests that failed, or:
test/partest test/files/<folder>/<testname>
This assumes you have previously done a:
test/partest --all
or called partest for some other test category.