Filtering tests
Rstest provides a variety of flexible ways to filter and select which test files and test cases to run. You can precisely control the test scope via configuration files, command-line arguments, and test APIs.
Filter by file name
Run all tests:
Run a specific test file:
Use glob patterns:
Run test files with foo in the name, such as foo.test.ts, foo/index.test.ts, foo-bar/index.test.ts, etc.:
You can also specify multiple test files or glob patterns:
Filter by file path
This method also applies to filtering by file path.
When filtering by file path, it can be either an absolute path or a relative path based on the current working directory (also called the project root directory or workspace root directory)
include/exclude
When you filter files directly with rstest **/*.test.ts, rstest will further filter files based on the include and exclude configuration.
You can modify the test file scope using the include and exclude options.
For example, to match test files named index in the test/a directory:
To match test files in test/a or test/b directories:
Filter by test name
If you only want to run test cases whose names contain a specific keyword, you can use testNamePattern.
For example, to only run test cases whose names contain "login":
Filter by project name
Rstest supports defining multiple test projects via projects. You can filter to run specific projects with the --project option.
For example, to match projects whose name is @test/a or @test/b:
You can also use wildcards to match project names:
You can exclude certain projects by negation:
Combined filtering
All filtering methods can be combined. For example:
In this case, rstest will only run test cases whose names contain login in all .test.ts files under the test directory, while excluding the test/legacy directory.
Common usage
-
Run a specific file only:
rstest test/foo.test.ts -
Run tests in a specific directory only:
rstest test/api/*.test.ts -
Exclude certain tests:
rstest --exclude test/legacy/** -
Run only tests whose names contain login:
rstest -t login -
Combined filtering:
rstest test/**/*.test.ts --exclude test/legacy/** --testNamePattern login
Filter via test API
Use the .only modifier to run only certain test suites or cases.
For example, only the test cases in suite A and case A will be run:
It should be noted that the .only flag only applies to the current test file. If you want to execute specific test cases within a specific file, you can use a combination of "filter by file name" and "filter via test API".
Use .skip or .todo to skip certain test suites or cases.