How to run Clojure tests from clj (not from lein neither boot)? - testing

In a project with the traditional lein project structure, how can I use simply clj to run the tests in the test folder?
Update:
After the mention the REPL, I would like to clarify I'm trying that from the System shell with the clj command. Not from the REPL, neither lein nor boot.

Check out the test-runner from Cognitect. https://github.com/cognitect-labs/test-runner
Once you add the alias, you should be able to run the tests via:
clj -Atest
If you need to configure the directory,
clj -Atest -d path/to/tests

You can run tests from the repl:
; all tests
(clojure.test/run-all-tests)
; all tests in one file
(clojure.test/run-tests 'com.myproject.test.routes.api_test)
; one particular test
(clojure.test/test-vars [#'com.myproject.test.routes.api_test/id-test])
You can find some additional information in official documentation.
And it's very convenient to run tests with cursive plugin in IntelliJ IDEA.

Related

CucumberOptions to execute feature file and Feature Folder using mvn

I am executing test using tags in the following command:-
mvn test -Dcucumber.options="--tags #smoketest (works fine)
I need to execute test using feature file and Feature folder which is not working
mvn test -Dcucumber.features="C:/Users/xxx/xxx/feature/orders.feature"
(not working, it does not override, i.e it picks up the tag mentioned in option tag )
mvn test -Dcucumber.options="C:/Users/xxx/xxx/feature/orders.feature"
(not working)
mvn test -Dcucumber.options="C:/Users/xxx/xxx/feature"(not working)
I need mvn execution command to run feature file and feature folder
Any help here will be greatly appreciated
Feature files need to be provided after the tags:
mvn test -Dcucumber.options="--tags #smoketest src/test/dev/path/to/feature"
EDIT:
I would use tags in the feature files to run specific features.
If you have features
amazon
harvest
mccoll
Mark each of them with corresponding tags:
#amazon
Feature: Amazon feature
#mccolls
Feature: McColls feature
and provide the path to all features:
mvn test -Dcucumber.options="--tags #amazon,#mccolls src/test/resources/features"
The only disclaimer here is: Be careful how do you provide tags in the command line. There are options for AND, OR, NOT operations. I used Cucumber a few years ago and I don't remember exactly which was which.
I BELIEVE (but be careful) that -t #amazon -t#mccolls is AND operation. Run tests tagged with #amazon AND #mccolls at the same time. So, both tags required.
To run them in OR fashion you'd have to use --tags #amazon,#mccolls
You can try it out with simple trial and error.

Unable to run tests through commandline [duplicate]

We have a multi module project with the following structure
module 1
module 2
module e2e
parent pom
The module e2e contains our karate features (into the src/test/java/features folder)
We couldn't figure out how to run the karate tests using the "mvn test".
It always runs 0 tests, instead there are some feature files.
We have tried running "mvn test" from the root of the project, as well as from inside the e2e module
We have other maven projects (not multi module) and it works as expected.
Does it necessary to make some configuration action to do it?
Thanks a lot.
mvn test behind the scenes just looks for JUnit tests, it is that simple. Check that your JUnit class names end with Test - and that the maven tweak for the recommended directory structure is in place: https://github.com/intuit/karate/issues/724
Otherwise unless you follow this process, it is difficult for anyone to help you: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Test executable failing only when run in ctest

When I use the ctest interface to cmake (add_test(...)), and run the make target make test, several of my tests fail. When I run each test directly at the command line from the binary build folder, they all work.
What can I use to debug this?
To debug, you can first run ctest directly instead of make test.
Then you can add the -V option to ctest to get verbose output.
A third neat trick from a cmake developer is to have ctest launch an xterm shell. So add
add_test(run_xterm xterm)
in your CMakeLists.txt file at the end. Then run make test and it will open up a xterm. Then see if you can replicate the test failing by running it from the xterm. If it does fail, then check your environment (i.e. run env > xterm.env from the xterm, then run it again env > regular.env from your normal session and diff the outputs).
I discovered that my tests were wired to look for external files passed on a relative path to the top of the binary cmake output folder (i.e. the one where you type make test). However, when you run the test through ctest, the current working directory is the binary folder for that particular subdirectory, and the test failed.
In other words:
This worked
test/mytest
but this didn't work
cd test; ./mytest
I had to fixed the unit tests to use an absolute path to the configuration files it needed, instead of a path like ../../../testvector/foo.txt.
The problem with ctest and googletest is that it assumes to run one command for each test case, whilst you will have potentially a lot of different test cases running in a single test executable. So when you use add_test with a Google Test executable, CTest reports one single failure whether actual number of failed test cases is 1 or 1000.
Since you say that running your test cases isolated makes them pass, my first suspicion is that your tests are somehow coupled. You can quickly check this by randomizing the test execution order using --gtest_shuffle, and see if you get the same failures.
I think the best approach to debug your failing test cases is not to use CTest, but just run the test executable using the command line options to filter the actual test cases getting run. I would start by running only the first test that fails together with the test run immediately before when the whole test suite is run.
Other useful tools to debug your test cases can be SCOPED_TRACE and extending your assertion messages with additional information.

IntelliJ 14 Gradle task in Test Runner

Upgraded to IntelliJ 14.0.1 One of the big new features I was looking for:
"If you run tests via a Gradle task, the IDE offers you the standard Test Runner instead of the console output." (Source: https://www.jetbrains.com/idea/whatsnew/#buildTools)
I right click on the Gradle Task to run our Integration Tests:
However, I see the results of the test still going to console output, not to the Test Runner:
Has anyone been able to get this new feature in IntelliJ IDEA 14 to work?
Thank you in advance,
Philip
Looks like IntelliJ looks for a task named "test" rather than a task of type Test.
https://github.com/JetBrains/intellij-community/blob/master/plugins/gradle/src/org/jetbrains/plugins/gradle/execution/test/runner/GradleTestsExecutionConsoleManager.java#L191
Rename the test task to unitTest and then create a wrapper that runs both:
// Rename test to unitTest
tasks.test.name = "unitTest"
// Wrap and run both
task test(dependsOn:['unitTest', 'integrationTest'])
If you only want to run integration tests, just overwrite it:
task test(overwrite: true, dependsOn: ['integrationTest'])
This allows me to run the integration tests in the test runner successfully (at least it works in IDEA 15 EAP but it should work in 14 as well I would think).
I still get this in IntelliJ 2017.1, but specifically when running tests in the gradle buildSrc directory. After digging a while, it turns out that the Gradle API doesn’t expose the test tasks in the special buildSrc directory to Intellij, so IntelliJ doesn’t recognize it as a test.
Workaround: Open another IntellIJ project for the buildSrc directory directory instead of trying to run tests cleanly inside the root project.

How to run Clojure code before each "lein test"?

How can I run some Clojure code before tests in test files are run?
I'd like to have some piece of Clojure code to be called either before running all the tests (say by doing lein test at the root of my lein project) or before running indidual tests. I don't want to duplicate that piece of code in several .clj files.
How can I do that? Is there some "special" .clj file that can be run before any test is run?
You probably want to use test fixtures. This question has a good answer on it that can get you started.