Skip to main content

TestEnv

Struct TestEnv 

Source
pub struct TestEnv { /* private fields */ }
Expand description

测试环境管理器

提供完整的测试生命周期管理,支持同步和异步钩子函数,多服务集成测试,以及容器化测试环境。

Implementations§

Source§

impl TestEnv

Source

pub fn new(config: TestEnvConfig) -> Self

创建新的测试环境

§Examples
use wae_testing::{TestEnv, TestEnvConfig};

let config = TestEnvConfig::default();
let env = TestEnv::new(config);
Source

pub fn default_env() -> Self

创建默认测试环境

§Examples
use wae_testing::TestEnv;

let env = TestEnv::default_env();
Source

pub fn setup(&self) -> TestingResult<()>

初始化测试环境

按顺序执行所有 before_setup 钩子、初始化环境、然后执行所有 after_setup 钩子。

§Errors

如果环境已初始化或任何钩子执行失败,将返回 WaeError 错误。

§Examples
use wae_testing::TestEnv;

let env = TestEnv::default_env();
env.setup().unwrap();
Source

pub async fn setup_async(&self) -> TestingResult<()>

异步初始化测试环境

异步执行所有生命周期钩子函数,适合需要异步初始化的场景。

§Errors

如果环境已初始化或任何钩子执行失败,将返回 WaeError 错误。

Source

pub fn teardown(&self) -> TestingResult<()>

清理测试环境

按顺序执行所有 before_teardown 钩子、清理资源、然后执行所有 after_teardown 钩子。

§Errors

如果环境未初始化或任何钩子执行失败,将返回 WaeError 错误。

§Examples
use wae_testing::TestEnv;

let env = TestEnv::default_env();
env.setup().unwrap();
env.teardown().unwrap();
Source

pub async fn teardown_async(&self) -> TestingResult<()>

异步清理测试环境

异步执行所有清理操作,适合需要异步清理的场景。

§Errors

如果环境未初始化或任何钩子执行失败,将返回 WaeError 错误。

Source

pub fn state(&self) -> TestEnvState

获取环境状态

§Examples
use wae_testing::{TestEnv, TestEnvState};

let env = TestEnv::default_env();
assert_eq!(env.state(), TestEnvState::Uninitialized);
Source

pub fn elapsed(&self) -> Duration

获取环境运行时间

返回从环境创建到现在经过的时间。

Source

pub fn initialized_elapsed(&self) -> Option<Duration>

获取环境初始化后运行的时间

如果环境尚未初始化,返回 None

Source

pub fn add_lifecycle_hook<H>(&self, hook: H)
where H: TestLifecycleHook + 'static,

注册同步生命周期钩子

添加一个同步钩子函数,在测试环境的各个生命周期阶段执行。

§Examples
use wae_testing::TestEnv;

struct MyHook;

impl wae_testing::TestLifecycleHook for MyHook {
    fn after_setup(&self, _env: &TestEnv) -> wae_testing::TestingResult<()> {
        println!("Environment setup complete!");
        Ok(())
    }
}

let env = TestEnv::default_env();
env.add_lifecycle_hook(MyHook);
Source

pub fn add_async_lifecycle_hook<H>(&self, hook: H)
where H: AsyncTestLifecycleHook + 'static,

注册异步生命周期钩子

添加一个异步钩子函数,在测试环境的各个生命周期阶段异步执行。

Source

pub fn on_cleanup<F>(&self, handler: F)
where F: Fn() + Send + Sync + 'static,

注册清理函数

添加一个同步清理函数,在测试环境清理时执行。清理函数按注册的逆序执行。

§Examples
use wae_testing::TestEnv;

let env = TestEnv::default_env();
env.on_cleanup(|| println!("Cleaning up!"));
Source

pub fn on_cleanup_async<F, Fut>(&self, handler: F)
where F: Fn() -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,

注册异步清理函数

添加一个异步清理函数,在测试环境清理时异步执行。

Source

pub fn set<T: 'static + Send + Sync>(&self, key: &str, value: T)

存储数据

在测试环境中存储任意类型的数据。

§Examples
use wae_testing::TestEnv;

let env = TestEnv::default_env();
env.set("test_key", "test_value");
Source

pub fn get<T: 'static + Clone>(&self, key: &str) -> Option<T>

获取数据

从测试环境中获取之前存储的数据。

§Examples
use wae_testing::TestEnv;

let env = TestEnv::default_env();
env.set("test_key", "test_value".to_string());
let value: Option<String> = env.get("test_key");
assert_eq!(value, Some("test_value".to_string()));
Source

pub fn remove<T: 'static>(&self, key: &str) -> Option<T>

移除数据

从测试环境中移除并返回之前存储的数据。

Source

pub fn has(&self, key: &str) -> bool

检查是否存在指定键的数据

Source

pub fn config(&self) -> &TestEnvConfig

获取配置

§Examples
use wae_testing::TestEnv;

let env = TestEnv::default_env();
let config = env.config();
assert_eq!(config.name, "test");
Source

pub fn add_service(&self, service_config: TestServiceConfig)

添加测试服务配置

向测试环境中添加一个服务配置,用于多服务集成测试。

Source

pub fn get_service(&self, name: &str) -> Option<TestServiceConfig>

获取测试服务配置

根据服务名称获取服务配置。

Source

pub fn enabled_services(&self) -> Vec<TestServiceConfig>

获取所有启用的服务

返回所有已启用的服务配置列表。

Source

pub async fn with_fixture<F, R>(&self, fixture: F) -> TestingResult<R>
where F: FnOnce() -> TestingResult<R>,

使用 fixture 运行测试

自动管理测试环境的初始化和清理,执行测试函数。

§Errors

如果环境初始化、测试执行或清理失败,将返回 WaeError 错误。

Source

pub async fn run_test<F, Fut>(&self, test: F) -> TestingResult<()>
where F: FnOnce() -> Fut, Fut: Future<Output = TestingResult<()>>,

运行异步测试

自动管理测试环境的初始化和清理,执行异步测试函数。

§Errors

如果环境初始化、测试执行或清理失败,将返回 WaeError 错误。

§Examples
use wae_testing::TestEnv;

let env = TestEnv::default_env();
env.run_test(|| async { Ok(()) }).await.unwrap();
Source

pub async fn run_test_async<F, Fut>(&self, test: F) -> TestingResult<()>
where F: FnOnce() -> Fut, Fut: Future<Output = TestingResult<()>>,

运行带异步生命周期的测试

使用异步初始化和清理运行测试,适合需要异步操作的测试场景。

§Errors

如果环境初始化、测试执行或清理失败,将返回 WaeError 错误。

Trait Implementations§

Source§

impl Debug for TestEnv

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for TestEnv

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.