跳到正文
cccskills
免费在 GitHub 公开中文介绍

writing-tests

为IntelliJ代码库编写和审查JUnit 5测试,指导选择对应测试模块、使用项目夹具与生命周期注解,并在明确的测试范围内处理协程和UI线程工作。

原文查看原文说明

Write IntelliJ JUnit 5 tests with fixtures, lifecycle, and EDT.

JetBrains/intellij-community星标 20,685(GitHub 20,685 + cccskills 0)

实测评分 未实测

查看安装方法

适用场景

  • 为IntelliJ模块添加JUnit 5测试
  • 配置测试用项目夹具
  • 检查测试中的UI线程和协程用法

中文介绍

功能

此技能用于为IntelliJ代码库编写和审查JUnit 5测试。它会指导你选择与生产模块对应的测试模块,并使用@TestApplication、项目夹具和@TestDisposable等方式组织测试。设置和清理使用JUnit 5生命周期注解;协程测试则以timeoutRunBlocking作为边界。它还会说明如何只在合适的调度器上执行UI操作,以及如何用完成信号代替固定等待时间。

适用场景

为IntelliJ模块添加测试,或审查现有测试的夹具、资源释放和UI线程使用时,可以使用此技能。它也适用于从JUnit 4风格迁移到JUnit 5,或编写包含协程的测试。

使用示例

  • “这个功能的测试应该放在哪个模块?”
  • “请编写一个使用项目夹具的JUnit 5测试。”
  • “请审查这个测试对UI线程和协程的处理。”

注意事项

适用范围是IntelliJ代码库中的测试。命令行运行示例为./tests.cmd --module <test-module> --test com.example.MyTest。

本介绍由 AI(OpenAI Codex)根据公开的 SKILL.md 撰写。准确的规格请查看下方原文。

文档检查

AI 根据 SKILL.md 的内容检查了需要注意的操作和使用前提。评分只依据实际运行的结果(实测对比)。

安全性发送数据、删除文件、执行命令等是否有说明
A良好内容以测试编写指导为主,没有描述删除或外部发送步骤。
安装难易度所需工具和 API 密钥是否少且有说明
B部分不足以IntelliJ代码库和测试模块为前提,但未提供安装步骤。

不计入评分。 由 AI(OpenAI Codex)于 2026年10月28日 评估。仅供参考,安装前请同时查看原文。

如果您认为评估有误,请申请复核

请写明哪一项有误以及原因。无需登录。我们会对照 SKILL.md 重新评估,并在此处更新结果。

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

包含的文件(1)

  • SKILL.md6.8 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

<!-- Generated by community/.ai/render-guides.mjs; edit community/.agents/skills/writing-tests/SKILL.md -->

Writing Tests

Guidelines for writing tests in IntelliJ IDEA codebase.

For examples, see community/platform/testFramework/junit5/test/showcase/.

Place Tests in the Owning Module

Put a test in the test module associated with the production module it exercises. Do not place it in a downstream module merely because that module has the production module on its test classpath. Check the production module's .iml file and neighboring tests before adding a new test.

In particular, code in org.jetbrains.intellij.build.io under community/build/tasks belongs to intellij.idea.community.build.tasks.tests (community/build/tasks/test), not intellij.platform.buildScripts.tests. The BUILD_SCRIPTS_PLATFORM_TESTS group deliberately excludes org.jetbrains.intellij.build.io.* to avoid matching build-task tests by class name across module boundaries. Putting such a test in the build-scripts test module leaves it outside every community test group and causes UltimateProjectTestsStructureTest to fail.

Prefer JUnit 5 over JUnit 4

Use JUnit 5 with @TestApplication annotation instead of extending LightJavaCodeInsightFixtureTestCase.

Why JUnit 5:

  • Faster: No class hierarchy overhead, shared fixtures via companion objects
  • Cleaner: Annotations (@TestDisposable, @RegistryKey) instead of manual setup/teardown
  • Flexible: Mix EDT and non-EDT tests in one class, parameterized tests, nested tests
  • Better isolation: Each test gets fresh disposables automatically

Shared Fixtures Pattern

Use companion object fixtures shared between all tests:

@TestApplication
internal class MyTest {
  companion object {
    private val projectFixture = projectFixture()
    private val moduleFixture = projectFixture.moduleFixture("src")
  }

  private val project get() = projectFixture.get()
  private val module get() = moduleFixture.get()
}

Lifecycle Hooks

Use JUnit 5 lifecycle annotations for setup and teardown:

@TestApplication
internal class MyTest {
  companion object {
    @JvmStatic
    @BeforeAll
    fun setUpClass() {
      // Once before all tests in class
    }

    @JvmStatic
    @AfterAll
    fun tearDownClass() {
      // Once after all tests in class
    }
  }

  @BeforeEach
  fun setUp() {
    // Before each test method
  }

  @AfterEach
  fun tearDown() {
    // After each test method
  }
}

Note: Prefer @TestDisposable over manual @AfterEach cleanup for resources.

Test Disposables

Use @TestDisposable annotation to inject test-scoped disposables (created before each test, disposed after):

@TestDisposable
lateinit var disposable: Disposable

// Or as parameter
@Test
fun myTest(@TestDisposable disposable: Disposable) { ... }

Registry Values in Tests

Use @RegistryKey annotation instead of Registry.get().setValue():

@Test
@RegistryKey(key = "my.registry.key", value = "true")
fun testWithRegistryEnabled() { ... }

System Properties in Tests

Use @SystemProperty annotation instead of System.setProperty():

@Test
@SystemProperty(propertyKey = "my.property", propertyValue = "value")
fun testWithSystemProperty() { ... }

Coroutines and UI Tests

Use com.intellij.testFramework.common.timeoutRunBlocking as the coroutine boundary. It has a 10-second default timeout, so a suspended test fails quickly with a coroutine-aware thread dump. Do not add a JUnit @Timeout to such a test. The two timeouts duplicate each other, and the @Timeout gives a worse diagnostic.

Keep setup, background work, and assertions off the UI thread. Move only Swing operations into a small withContext(Dispatchers.UI) block:

@Test
fun updatesLabel(): Unit = timeoutRunBlocking {
  val value = loadValue()
  val actual = withContext(Dispatchers.UI) {
    label.text = value
    label.text
  }
  assertThat(actual).isEqualTo(value)
}

Dispatchers.UI is strict: it supplies UI-thread affinity without implicit model access. Use Dispatchers.EDT only when the tested operation genuinely requires model or lock access and cannot be split from the UI operation. Keep write actions explicit.

Editor creation/disposal, editor document mutation, completion invocation, action-group expansion, and file-editor operations are common model-backed exceptions. Keep their full synchronous lifecycle in a narrow Dispatchers.EDT block; do not construct on strict UI and dispose later from a different dispatcher.

Do not use @RunInEdt, @RunMethodInEdt, runInEdtAndWait, or runInEdtAndGet in new tests. They hide the coroutine boundary, move lifecycle methods and assertions onto EDT, and can accidentally grant model or write-intent access.

When a synchronous callback API cannot call a suspending function, use a narrow bounded adapter around that callback only:

timeoutRunBlocking(timeout = 10.seconds, context = Dispatchers.UI) {
  createSwingComponent()
}

Prefer observable completion signals, flows, latches, or virtual time over sleeps. If polling is unavoidable, bound it and make the predicate suspending so UI checks can use withContext(Dispatchers.UI) without nested blocking.

Use the enclosing test scope for launched work: pass this from timeoutRunBlocking/coroutineScope, or backgroundScope from runTest, into the code under test. Create a standalone CoroutineScope(...) only when independent scope lifetime is itself under test; parent it to the test job and cancel it in finally. Use delay freely with runTest virtual time, but do not use a real timing-only delay as a completion signal.

Key Classes

  • com.intellij.testFramework.junit5.TestApplication - initializes shared application
  • com.intellij.testFramework.junit5.TestDisposable - injects test disposables
  • com.intellij.testFramework.junit5.RegistryKey - sets registry values
  • com.intellij.testFramework.junit5.SystemProperty - sets system properties
  • com.intellij.testFramework.common.timeoutRunBlocking - runs suspending test code with a 10-second default timeout
  • com.intellij.testFramework.junit5.fixture.projectFixture - creates project fixtures
  • com.intellij.testFramework.junit5.fixture.moduleFixture - creates module fixtures

Showcase Tests

  • JUnit5ProjectFixtureTest.kt - project fixture patterns
  • JUnit5DisposableTest.kt - disposable injection
  • JUnit5SystemPropertyTest.kt - system property usage
  • JUnit5RunInEdtTest.java - legacy EDT extension behavior; do not copy it for new tests

Running Tests

To run tests via command line, see TESTING.md.

Quick example:

./tests.cmd --module <test-module> --test com.example.MyTest

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

actions

免费有简介

实现或修改 IntelliJ `AnAction`,并在 `plugin.xml` 中注册。将显示名称和说明写入消息资源文件,按需设置图标,并通过事件协程作用域启动挂起任务,避免同步处理阻塞 IDE。

  • 为 IDE 菜单添加操作
  • 修改操作注册或图标
  • 整理操作名称和说明
JetBrains/intellij-community2.1万2026年10月29日 更新

bazel-test-migration

免费有简介

将 IntelliJ JPS 测试模块迁移到 Bazel,并排查仅在 Bazel 中出现的失败。根据症状检查依赖、插件、测试数据和 runfiles,重新生成并验证目标。

  • 将 JPS 测试模块迁移到 Bazel
  • 排查仅在 Bazel 中失败的测试
  • 检查测试依赖和数据配置
JetBrains/intellij-community2.1万2026年10月29日 更新

code-style

免费有简介

按 IntelliJ 项目规范编写或审查 Kotlin 和 Java 代码,涵盖语言选择、格式、密封类型、线程注解及控制流异常处理。

  • 按 IntelliJ 规范编写 Kotlin
  • 审查 Java 代码
  • 检查异常处理和密封类型分支
JetBrains/intellij-community2.1万2026年10月29日 更新

commits

免费有简介

本指南帮助您按 IntelliJ 项目规则拆分更改并撰写提交信息:有工单时使用编号和变更说明,无工单时使用标签与子系统名,并在需要时说明原因和决定。

  • 按项目格式编写带工单号的提交信息
  • 整理测试或文档提交并选择合适标签
  • 按规则修改已有提交信息
JetBrains/intellij-community2.1万2026年10月29日 更新

使用多个 Python 类型检查器比较代码片段或文件的类型推断和报错判断,并汇总各工具的结论、耗时与诊断信息,可保存为 Markdown 报告。

  • 比较不同检查器的类型推断
  • 确认代码是否会被判为类型错误
  • 查看多个工具的诊断差异
JetBrains/intellij-community2.1万2026年10月29日 更新

整理 Compose Desktop 界面中的状态和副作用,让可组合函数负责绘制,由展示器或协调器处理应用决策。了解状态归属、Effect、可复用组件接口、延迟列表键、动画和性能,适用于单项界面修改。

  • 分离界面绘制与展示器决策
  • 整理界面状态的归属
  • 检查 Effect 键和协程用法
JetBrains/intellij-community2.1万2026年10月29日 更新

查看 JetBrains 的全部技能

报告此技能的问题