Run `cargo test --workspace` and exclude one test - testing

I have a workspace with several crates. I need to exclude a particular test.
I tried adding an environment variable check, but this doesn't work. I guess cargo test filters out the environment variables.
// package1/src/lib.rs
// ...
#[cfg(test)]
mod tests {
#[test]
fn test1() {
if std::env::var("CI").is_ok() {
return;
}
// ...
}
}
Then I tried passing the --exclude parameter with various options, but none of them work:
cargo test --workspace --exclude test1
cargo test --workspace --exclude tests:test1
cargo test --workspace --exclude tests::test1
cargo test --workspace --exclude '*test1'
cargo test --workspace --exclude 'tests*test1'
cargo test --workspace --exclude package1
This skips all of the tests in the package.
cargo test --workspace --exclude 'package1*test1'
How can I run all workspace tests except one?

Excluding a test
The help file by running cargo test -- --help lists useful options:
--skip FILTER Skip tests whose names contain FILTER (this flag can
be used multiple times)
Regarding the -- after test, see:
What does “--” (double-dash) mean?
src/lib.rs
fn add(a: u64, b: u64) -> u64 {
a + b
}
fn mul(a: u64, b: u64) -> u64 {
a * b
}
#[cfg(test)]
mod tests {
use super::{add, mul};
#[test]
fn test_add() {
assert_eq!(add(21, 21), 42);
}
#[test]
fn test_mul() {
assert_eq!(mul(21, 2), 42);
}
}
Runing the above with cargo test -- --skip test_mul will give the following output:
running 1 test
test tests::test_add ... ok
Excluding a test within a specific package
If you want to exclude a specific test for package within a workspace, you can do so in the following way, replacing my_package and my_test with their appropriate names:
Test all, but exclude my_package
cargo test --workspace --exclude my_package
And then test my_package itself, with the specific test excluded by adding --skip my_test:
cargo test --package my_package -- --skip my_test
For more options, see:
The Cargo Book, "cargo-test(1)"
Excluding a test by default
Alternatively, you could add the #[ignore] attribute to tests that should not run by default. You can still run them separately if you wish to do so:
src/lib.rs
#[test]
#[ignore]
fn test_add() {
assert_eq!(add(21, 21), 42);
}
Running the tests using cargo test -- --ignored:
running 1 test
test tests::test_add ... ok
If you're using Rust >= 1.51 and want to run all tests, including those marked with the #[ignore] attribute, you can pass --include-ignored.

Related

gradle test: how to run one method?

How to run just one method of a test case for debugging with Gradle? I have tried:
gradle test -tests example.TestFoo#testMethod1 --debug-jvm
but it ends up with following error:
No tests found for given includes: example.TestFoo#testMethod1
The test TestFoo class has testMethod1(), testMethod2(), etc.
Use . instead # in your tests filter expression to point to a method name:
gradle test --tests example.TestFoo.testMethod1 --debug-jvm
You can find more examples on filtering tests in 48.14.3. Test filtering documentation section.

IntelliJ run TestNG tests in 1 directory

I have separated my unit and integration tests into separate IntelliJ "Test Sources" directories. When I right-click on my unit test folder and attempt to run those tests, the integration tests get roped in as well.
I would ultimately like to be able to right-click on my unit test folder and have only the tests under that folder run and the same for integration tests.
Is there a way to do this in IntelliJ or am I going to have to use a testng.xml file to accomplish this?
How about using 2 test groups and creating 2 run configurations, 1 for unit testing and 1 for integration testing:
1) Some dummy test class that simulates both categories
import org.testng.annotations.Test;
public class CategoryTests {
#Test(groups = "unit")
public void someUnitTest(){
}
#Test(groups = "integration")
public void someIntegrationTest(){
}
}
2) Unit test IJ run config (notice the Group setup)
3) Integration test IJ run config (again, notice the Group setup)
4) All in one sample

How do I run Serenity web tests in parallel with gradle?

Can't figure out how to run Serenity web tests in parallel with gradle.
Here is an example with maven + jenkins. But I need the same thing with gradle.
you can do this by following the steps
Step 1: Create Suite file
Step 2: Enter the following task code in gradle
task runAParallelSuite(type: Test) {
def forks =2
exclude ('**/Library.java')
println "The Maximum parallel is $forks"
// uncomment maxParallelForks if you prefer to use the Gradle process forker
// which also requires a complete change of how the suite class works
maxParallelForks = forks
include '**/**TestSuite.class'
// testReportDir = file("${reporting.baseDir}/AParallelSuite")
// testResultsDir = file("${buildDir}/test-results/AParallelSuite")
// show standard out and standard error of the test JVM(s) on the console
testLogging.showStandardStreams = true
}
now run the command in cmd prompt 'gradle clean runAParallelSuite aggregate'
Here is another way to do this
test {
maxParallelForks=2
options {
systemProperties(System.getProperties())
}
...
}
maxParallelForks allows to set maximum number of forked test processes to execute in parallel with jUnit

configure Robolectric to write logs (tests output) into external file

I'm adding the following code to build.gradle :
android{
testOptions {
unitTests.all {
systemProperty 'robolectric.logging', '/path/to/file/robolectric.log'
systemProperty 'robolectric.logging.enabled', 'true'
}
}
}
The file /path/to/file/robolectric.log has 777 permissions.
The robolectric.logging.enabled=true is working because I started to see the output when I had configured with robolectric.logging=stdout (like here)
Questions:
1) How to configure Robolectric to write the output into a file ?
2) How to configure Robolectric to write the output into something like build/output/robolectric.log? The test cases are executed by Jenkins and I would like to have the option to have the robotic logging for each execution.

run single integration test with gradle

I'm trying to run a single integration tests using gradle's -Dtest.single flag. I have added another source set, src/integrationTest and put the tests in there. I have an integration test task
task integrationTests(type: Test) {
dependsOn 'assemble', 'integrationTestClasses'
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
}
This runs fine, but if I try to run a single test it tells me it cannot find a matching test. I don't want to have to run every integration test each time I am writing a new one. Is there a way to do this?
Since Gradle 1.10 you can write:
//select specific test method
gradle test --tests org.gradle.SomeTest.someFeature
//select specific test class
gradle test --tests org.gradle.SomeTest
//select all tests from package
gradle test --tests org.gradle.internal*
//select all ui test methods from integration tests by naming convention
gradle test --tests *IntegTest*ui*
//selecting tests from different test tasks
gradle test --tests *UiTest integTest --tests *WebTest*ui
Read more here
http://www.gradle.org/docs/1.10/release-notes#executing-specific-tests-from-the-command-line
The correct syntax is:
gradle testTaskName -DtestTaskName.single=...
In this case:
gradle integrationTest -DintegrationTest.single=...
Just incase anyone is coming here looking for answers. This has been removed in gradle 5.0. Look for test.single in https://docs.gradle.org/current/userguide/upgrading_version_4.html
If you still wish to use a command line option in this style you should be able to use the --tests commandline param. See https://docs.gradle.org/current/userguide/java_testing.html#simple_name_pattern
$ ./gradlew integrationTest --tests=MyTest