gpui-test

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Overview

概述

GPUI provides a comprehensive testing framework that allows you to test UI components, async operations, and distributed systems. Tests run on a single-threaded executor that provides deterministic execution and the ability to test complex async scenarios. GPUI tests use the
#[gpui::test]
attribute and work with
TestAppContext
for basic testing and
VisualTestContext
for window-dependent tests.
GPUI 提供了一套全面的测试框架,可用于测试UI组件、异步操作和分布式系统。测试在单线程执行器上运行,可提供确定性执行能力,支持测试复杂的异步场景。GPUI 测试使用
#[gpui::test]
属性,基础测试可借助
TestAppContext
,依赖窗口的测试则使用
VisualTestContext

Rules

规则

  • If test does not require windows or rendering, we can avoid use
    [gpui::test]
    and
    TestAppContext
    , just write simple rust test.
  • 如果测试不需要窗口或渲染,无需使用
    #[gpui::test]
    TestAppContext
    ,只需编写简单的Rust测试即可。

Core Testing Infrastructure

核心测试基础设施

Test Attributes

测试属性

Basic Test

基础测试

rust
#[gpui::test]
fn my_test(cx: &mut TestAppContext) {
    // Test implementation
}
rust
#[gpui::test]
fn my_test(cx: &mut TestAppContext) {
    // Test implementation
}

Async Test

异步测试

rust
#[gpui::test]
async fn my_async_test(cx: &mut TestAppContext) {
    // Async test implementation
}
rust
#[gpui::test]
async fn my_async_test(cx: &mut TestAppContext) {
    // Async test implementation
}

Property Test with Iterations

带迭代次数的属性测试

rust
#[gpui::test(iterations = 10)]
fn my_property_test(cx: &mut TestAppContext, mut rng: StdRng) {
    // Property testing with random data
}
rust
#[gpui::test(iterations = 10)]
fn my_property_test(cx: &mut TestAppContext, mut rng: StdRng) {
    // Property testing with random data
}

Test Contexts

测试上下文

TestAppContext

TestAppContext

TestAppContext
provides access to GPUI's core functionality without windows:
rust
#[gpui::test]
fn test_entity_operations(cx: &mut TestAppContext) {
    // Create entities
    let entity = cx.new(|cx| MyComponent::new(cx));

    // Update entities
    entity.update(cx, |component, cx| {
        component.value = 42;
        cx.notify();
    });

    // Read entities
    let value = entity.read_with(cx, |component, _| component.value);
    assert_eq!(value, 42);
}
TestAppContext
可在不启动窗口的情况下访问GPUI的核心功能:
rust
#[gpui::test]
fn test_entity_operations(cx: &mut TestAppContext) {
    // Create entities
    let entity = cx.new(|cx| MyComponent::new(cx));

    // Update entities
    entity.update(cx, |component, cx| {
        component.value = 42;
        cx.notify();
    });

    // Read entities
    let value = entity.read_with(cx, |component, _| component.value);
    assert_eq!(value, 42);
}

VisualTestContext

VisualTestContext

VisualTestContext
extends
TestAppContext
with window support:
rust
#[gpui::test]
fn test_with_window(cx: &mut TestAppContext) {
    // Create window with component
    let window = cx.update(|cx| {
        cx.open_window(Default::default(), |_, cx| {
            cx.new(|cx| MyComponent::new(cx))
        }).unwrap()
    });

    // Convert to visual context
    let mut cx = VisualTestContext::from_window(window.into(), cx);

    // Access window and component
    let component = window.root(&mut cx).unwrap();
}
VisualTestContext
扩展了
TestAppContext
的功能,支持窗口操作:
rust
#[gpui::test]
fn test_with_window(cx: &mut TestAppContext) {
    // Create window with component
    let window = cx.update(|cx| {
        cx.open_window(Default::default(), |_, cx| {
            cx.new(|cx| MyComponent::new(cx))
        }).unwrap()
    });

    // Convert to visual context
    let mut cx = VisualTestContext::from_window(window.into(), cx);

    // Access window and component
    let component = window.root(&mut cx).unwrap();
}

Additional Resources

额外资源

  • For detailed testing patterns and examples, see reference.md
  • For best practices and running tests, see examples.md
  • 如需了解详细的测试模式和示例,请查看reference.md
  • 如需了解最佳实践和测试运行方法,请查看examples.md