feat: Specify tags for JUnit 5 tests (#1471)

* feat: Specify tags for JUnit 5 tests

- to include a tag, simply add that tag name.
- to exclude a tag, use '!' as the prefix. E.g. '!develop'.

Signed-off-by: Sheng Chen <sheche@microsoft.com>
This commit is contained in:
Sheng Chen 2022-08-26 13:54:13 +08:00 коммит произвёл GitHub
Родитель 0ea75f5ab9
Коммит 51463e7705
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 181 добавлений и 43 удалений

Просмотреть файл

@ -284,6 +284,21 @@
"type": "string",
"description": "%configuration.java.test.config.preLaunchTask.description%",
"default": ""
},
"filters": {
"type": "object",
"description": "%configuration.java.test.config.filters.description%",
"default": {},
"properties": {
"tags": {
"type": "array",
"items": {
"type": "string"
},
"markdownDescription": "%configuration.java.test.config.filters.tags.description%",
"default": []
}
}
}
},
"description": "%configuration.java.test.config.description%",
@ -388,6 +403,21 @@
"type": "string",
"description": "%configuration.java.test.config.preLaunchTask.description%",
"default": ""
},
"filters": {
"type": "object",
"description": "%configuration.java.test.config.filters.description%",
"default": {},
"properties": {
"tags": {
"type": "array",
"items": {
"type": "string"
},
"markdownDescription": "%configuration.java.test.config.filters.tags.description%",
"default": []
}
}
}
}
},
@ -435,7 +465,7 @@
"lodash": "^4.17.21",
"lru-cache": "^7.13.1",
"reflect-metadata": "^0.1.13",
"vscode-extension-telemetry-wrapper": "^0.13.1",
"vscode-extension-telemetry-wrapper": "^0.13.2",
"vscode-languageclient": "6.0.0-next.9",
"vscode-tas-client": "^0.1.47"
}

Просмотреть файл

@ -1,32 +1,34 @@
{
"description": "Run and debug JUnit or TestNG test cases",
"description": "Run and debug JUnit or TestNG test cases.",
"contributes.commands.java.test.editor.run.title": "Run Tests",
"contributes.commands.java.test.editor.debug.title": "Debug Tests",
"contributes.commands.java.test.runFromJavaProjectExplorer.title": "Run Tests",
"contributes.commands.java.test.debugFromJavaProjectExplorer.title": "Debug Tests",
"contributes.commands.java.test.goToTest.title": "Go to Test",
"contributes.commands.java.test.goToTestSubject.title": "Go to Test Subject",
"configuration.java.test.defaultConfig.description": "Specify the name of the default test configuration",
"configuration.java.test.config.description": "Specify the configurations for running the tests",
"configuration.java.test.config.item.description": "Specify the configuration item for running the tests",
"configuration.java.test.config.name.description": "Specify the name of the configuration item",
"configuration.java.test.config.workingDirectory.description": "Specify the working directory when running the tests",
"configuration.java.test.config.classPaths.description": "The classpaths defined in this setting will be appended to the resolved classpaths",
"configuration.java.test.config.classPaths.auto.description": "Automatically resolve the classpaths of current project",
"configuration.java.test.config.classPaths.runtime.description": "The classpaths within 'runtime' scope of current project",
"configuration.java.test.config.classPaths.test.description": "The classpaths within 'test' scope of current project",
"configuration.java.test.config.classPaths.exclude.description": "The path after '!' will be excluded from the classpaths",
"configuration.java.test.config.modulePaths.description": "The modulepaths defined in this setting will be appended to the resolved modulepaths",
"configuration.java.test.config.modulePaths.auto.description": "Automatically resolve the modulepaths of current project",
"configuration.java.test.config.modulePaths.runtime.description": "The modulepaths within 'runtime' scope of current project",
"configuration.java.test.config.modulePaths.test.description": "The modulepaths within 'test' scope of current project",
"configuration.java.test.config.modulePaths.exclude.description": "The path after '!' will be excluded from the modulePaths",
"configuration.java.test.config.vmArgs.description": "Specify the extra options and system properties for the JVM",
"configuration.java.test.config.args.description": "Specify the command line arguments which will be passed to the test runner",
"configuration.java.test.config.env.description": "Specify the extra environment variables when running the tests",
"configuration.java.test.defaultConfig.description": "Specify the name of the default test configuration.",
"configuration.java.test.config.description": "Specify the configurations for running the tests.",
"configuration.java.test.config.item.description": "Specify the configuration item for running the tests.",
"configuration.java.test.config.name.description": "Specify the name of the configuration item.",
"configuration.java.test.config.workingDirectory.description": "Specify the working directory when running the tests.",
"configuration.java.test.config.classPaths.description": "The classpaths defined in this setting will be appended to the resolved classpaths.",
"configuration.java.test.config.classPaths.auto.description": "Automatically resolve the classpaths of current project.",
"configuration.java.test.config.classPaths.runtime.description": "The classpaths within 'runtime' scope of current project.",
"configuration.java.test.config.classPaths.test.description": "The classpaths within 'test' scope of current project.",
"configuration.java.test.config.classPaths.exclude.description": "The path after '!' will be excluded from the classpaths.",
"configuration.java.test.config.modulePaths.description": "The modulepaths defined in this setting will be appended to the resolved modulepaths.",
"configuration.java.test.config.modulePaths.auto.description": "Automatically resolve the modulepaths of current project.",
"configuration.java.test.config.modulePaths.runtime.description": "The modulepaths within 'runtime' scope of current project.",
"configuration.java.test.config.modulePaths.test.description": "The modulepaths within 'test' scope of current project.",
"configuration.java.test.config.modulePaths.exclude.description": "The path after '!' will be excluded from the modulePaths.",
"configuration.java.test.config.vmArgs.description": "Specify the extra options and system properties for the JVM.",
"configuration.java.test.config.args.description": "Specify the command line arguments which will be passed to the test runner.",
"configuration.java.test.config.env.description": "Specify the extra environment variables when running the tests.",
"configuration.java.test.config.envFile.description": "Specify the absolute path to a file containing environment variable definitions.",
"configuration.java.test.config.sourcePaths.description": "Specify extra source paths when debugging the tests",
"configuration.java.test.config.sourcePaths.description": "Specify extra source paths when debugging the tests.",
"configuration.java.test.config.preLaunchTask.description": "Specify the label of a task specified in tasks.json (in the workspace's .vscode folder). The task will be launched before the start of testing.",
"configuration.java.test.config.filters.description": "Specify the test filters.",
"configuration.java.test.config.filters.tags.description": "Specify the tags to be included or excluded. \n\nTags having `!` as the prefix will be **excluded**. \n\nNote: It **only** takes effect on JUnit 5 tests.",
"contributes.viewsWelcome.inLightWeightMode": "No test cases are listed because the Java Language Server is currently running in [LightWeight Mode](https://aka.ms/vscode-java-lightweight). To show test cases, click on the button to switch to Standard Mode.\n[Switch to Standard Mode](command:java.server.mode.switch?%5B%22Standard%22,true%5D)",
"contributes.viewsWelcome.enableTests": "Click below button to configure a test framework for your project.\n[Enable Java Tests](command:_java.test.enableTests)"
}

Просмотреть файл

@ -6,27 +6,29 @@
"contributes.commands.java.test.debugFromJavaProjectExplorer.title": "调试测试",
"contributes.commands.java.test.goToTest.title": "转到测试",
"contributes.commands.java.test.goToTestSubject.title": "转到被测试代码",
"configuration.java.test.defaultConfig.description": "设定默认测试配置项的名称",
"configuration.java.test.config.description": "设定运行测试的配置信息",
"configuration.java.test.config.item.description": "设定运行测试时所用的配置项",
"configuration.java.test.config.name.description": "命名配置项",
"configuration.java.test.config.workingDirectory.description": "设定执行测试时的工作目录",
"configuration.java.test.config.classPaths.description": "通过该配置项设置的类路径会被追加到自动解析的类路径上",
"configuration.java.test.config.classPaths.auto.description": "自动从当前工程中解析类路径",
"configuration.java.test.config.classPaths.runtime.description": "当前工程中属于 runtime 作用域的类路径",
"configuration.java.test.config.classPaths.test.description": "当前工程中属于 test 作用域的类路径",
"configuration.java.test.config.classPaths.exclude.description": "'!' 之后的路径将会从类路径中去除",
"configuration.java.test.config.modulePaths.description": "通过该配置项设置的模块路径会被追加到自动解析的模块路径上",
"configuration.java.test.config.modulePaths.auto.description": "自动从当前工程中解析模块路径",
"configuration.java.test.config.modulePaths.runtime.description": "当前工程中属于 runtime 作用域的模块路径",
"configuration.java.test.config.modulePaths.test.description": "当前工程中属于 test 作用域的模块路径",
"configuration.java.test.config.modulePaths.exclude.description": "'!' 之后的路径将会从模块路径中去除",
"configuration.java.test.config.vmArgs.description": "设定启动 JVM 的额外选项和系统属性",
"configuration.java.test.config.args.description": "设定启动 Test Runner 时的命令行参数",
"configuration.java.test.config.env.description": "启动应用程序时自定义的环境变量",
"configuration.java.test.config.envFile.description": "环境变量文件绝对路径",
"configuration.java.test.config.sourcePaths.description": "设定调试测试用例时的源代码路径",
"configuration.java.test.config.preLaunchTask.description": "在 tasks.json在工作空间的.vscode文件夹中中某个任务的名称。该任务会在启动测试之前被执行",
"configuration.java.test.defaultConfig.description": "设定默认测试配置项的名称。",
"configuration.java.test.config.description": "设定运行测试的配置信息。",
"configuration.java.test.config.item.description": "设定运行测试时所用的配置项。",
"configuration.java.test.config.name.description": "命名配置项。",
"configuration.java.test.config.workingDirectory.description": "设定执行测试时的工作目录。",
"configuration.java.test.config.classPaths.description": "通过该配置项设置的类路径会被追加到自动解析的类路径上。",
"configuration.java.test.config.classPaths.auto.description": "自动从当前工程中解析类路径。",
"configuration.java.test.config.classPaths.runtime.description": "当前工程中属于 runtime 作用域的类路径。",
"configuration.java.test.config.classPaths.test.description": "当前工程中属于 test 作用域的类路径。",
"configuration.java.test.config.classPaths.exclude.description": "'!' 之后的路径将会从类路径中去除。",
"configuration.java.test.config.modulePaths.description": "通过该配置项设置的模块路径会被追加到自动解析的模块路径上。",
"configuration.java.test.config.modulePaths.auto.description": "自动从当前工程中解析模块路径。",
"configuration.java.test.config.modulePaths.runtime.description": "当前工程中属于 runtime 作用域的模块路径。",
"configuration.java.test.config.modulePaths.test.description": "当前工程中属于 test 作用域的模块路径。",
"configuration.java.test.config.modulePaths.exclude.description": "'!' 之后的路径将会从模块路径中去除。",
"configuration.java.test.config.vmArgs.description": "设定启动 JVM 的额外选项和系统属性。",
"configuration.java.test.config.args.description": "设定启动 Test Runner 时的命令行参数。",
"configuration.java.test.config.env.description": "启动应用程序时自定义的环境变量。",
"configuration.java.test.config.envFile.description": "环境变量文件绝对路径。",
"configuration.java.test.config.sourcePaths.description": "设定调试测试用例时的源代码路径。",
"configuration.java.test.config.preLaunchTask.description": "在 tasks.json在工作空间的.vscode文件夹中中某个任务的名称。该任务会在启动测试之前被执行。",
"configuration.java.test.config.filters.description": "测试过滤配置项。",
"configuration.java.test.config.filters.tags.description": "指定要包含或排除的标记。\n\n带有`!`前缀的标记将会被**排除**。 \n\n注意该选项**仅**会对 JUnit 5 生效。",
"contributes.viewsWelcome.inLightWeightMode": "由于 Java 语言服务正运行在 [LightWeight 模式](https://aka.ms/vscode-java-lightweight)下,因此测试用例将不会展示在该视图中。如果您需要展示测试用例,可以点击下方按钮将 Java 语言服务切换至 Standard 模式。\n[切换至 Standard 模式](command:java.server.mode.switch?%5B%22Standard%22,true%5D)",
"contributes.viewsWelcome.enableTests": "点击下方按钮为你的项目添加一个测试框架\n[启用 Java 测试](command:_java.test.enableTests)"
}

Просмотреть файл

@ -70,6 +70,19 @@ export interface IExecutionConfig {
* @since 0.33.0
*/
preLaunchTask?: string;
/**
* The configurations for test filters.
* @since 0.37.0
*/
filters?: {
/**
* The test tags which will be included or excluded when running tests.
* This field will only take effect on JUnit 5 tests.
* @since 0.37.0
*/
tags?: string[]
}
}
export function getBuiltinConfig(): IExecutionConfig {

Просмотреть файл

@ -64,7 +64,10 @@ export async function resolveLaunchConfigurationForRunner(runner: BaseRunner, te
...config?.modulePaths || [],
...launchArguments.modulepath || [],
],
args: launchArguments.programArguments,
args: [
...launchArguments.programArguments,
...(testContext.kind === TestKind.JUnit5 ? parseTags(config) : [])
],
vmArgs: launchArguments.vmArguments,
env: config?.env,
envFile: config?.envFile,
@ -136,3 +139,40 @@ async function resolveJUnitLaunchArguments(projectName: string, testLevel: TestL
return argument;
}
/**
* Parse the tags from the test configuration.
*/
function parseTags(config: IExecutionConfig | undefined): string[] {
const tags: string[] = []
if (config?.filters?.tags) {
for (let tag of config.filters.tags) {
tag = tag.trim();
const isExcluded: boolean = tag.startsWith('!');
if (isExcluded) {
tag = tag.slice(1);
}
if (tag.length === 0) {
continue;
}
if (isExcluded) {
tags.push('--exclude-tag');
} else {
tags.push('--include-tag');
}
tags.push(tag);
}
}
if (tags.length) {
sendInfo('', {
testFilters: 'tags'
});
}
return tags;
}
// tslint:disable-next-line: typedef
export const exportedForTesting = {
parseTags
}

Просмотреть файл

@ -9,6 +9,7 @@ import { IJUnitLaunchArguments } from "../../src/runners/baseRunner/BaseRunner";
import { TestKind, TestLevel } from "../../src/types";
import { executeJavaLanguageServerCommand } from "../../src/utils/commandUtils";
import { setupTestEnv } from "./utils";
import { exportedForTesting } from "../../src/utils/launchUtils";
// tslint:disable: only-arrow-functions
// tslint:disable: no-object-literal-type-assertion
@ -77,4 +78,30 @@ suite('LaunchUtils Tests', () => {
'junit5.ParameterizedAnnotationTest:testCheckUser(junit5.ParameterizedAnnotationTest$User)'
);
});
test('test parseTags()', () => {
const { parseTags } = exportedForTesting;
const config = {
filters: {
tags: [
"foo",
"!bar",
"develop",
"!production"
]
}
};
const tags: string[] = parseTags(config);
const expected: string[] = [
"--include-tag",
"foo",
"--exclude-tag",
"bar",
"--include-tag",
"develop",
"--exclude-tag",
"production"
]
assert.ok(tags.length === expected.length && tags.every((tag, idx) => tag === expected[idx]));
});
});

Просмотреть файл

@ -0,0 +1,24 @@
package junit5.tags;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
@Tag("unit")
interface IBase {}
@Tag("integration")
class Base {}
public class TagTest extends Base implements IBase {
@Test
@Tag("fast")
void fastTest() {
}
@Test
@Tag("slow")
void slowTest() throws InterruptedException {
Thread.sleep(2000);
}
}