Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 有没有一种简单的方法可以有条件地启用或忽略Rust中的整个测试套件?_Unit Testing_Rust - Fatal编程技术网

Unit testing 有没有一种简单的方法可以有条件地启用或忽略Rust中的整个测试套件?

Unit testing 有没有一种简单的方法可以有条件地启用或忽略Rust中的整个测试套件?,unit-testing,rust,Unit Testing,Rust,我正在开发一个Rust库,它提供对一些硬件设备的访问。有两种设备类型,1和2,类型2的功能是类型1功能的超集 我想针对不同的情况提供不同的测试套件: 没有连接设备的测试(基本的健全性检查,例如CI服务器) 测试共享功能(需要类型为1或2的设备) 类型2专用功能测试(需要类型2的设备) 我使用特性来表示这种行为:默认特性测试无设备和可选特性测试类型一和测试类型二。然后我使用cfg\u attr属性忽略基于所选功能的测试: #[test] #[cfg_attr(not(feature = "te

我正在开发一个Rust库,它提供对一些硬件设备的访问。有两种设备类型,1和2,类型2的功能是类型1功能的超集

我想针对不同的情况提供不同的测试套件:

  • 没有连接设备的测试(基本的健全性检查,例如CI服务器)
  • 测试共享功能(需要类型为1或2的设备)
  • 类型2专用功能测试(需要类型2的设备)
我使用特性来表示这种行为:默认特性
测试无设备
和可选特性
测试类型一
测试类型二
。然后我使用
cfg\u attr
属性忽略基于所选功能的测试:

#[test]
#[cfg_attr(not(feature = "test-type-two"), ignore)]
fn test_exclusive() {
    // ...
}

#[test]
#[cfg_attr(not(any(feature = "test-type-two", feature = "test-type-one")), ignore)]
fn test_shared() {
    // ...
}
这是相当麻烦的,因为我必须在每次测试中重复这个条件,并且这些条件很难读取和维护

有没有更简单的方法来管理测试套件

我试图在声明模块时设置
ignore
属性,但显然它只能为每个
test
函数设置。我想我可以通过在模块上使用
cfg
来禁用已排除测试的编译,但是由于测试应该始终编译,我希望避免这种情况

有没有一种简单的方法可以有条件地启用或忽略Rust中的整个测试套件

最简单的方法是不编译测试:

#[cfg(test)]
mod test {
    #[test]
    fn no_device_needed() {}

    #[cfg(feature = "test1")]
    mod test1 {
        fn device_one_needed() {}
    }

    #[cfg(feature = "test2")]
    mod test2 {
        fn device_two_needed() {}
    }
}
我必须在每次测试中重复这个条件,这些条件很难读取和维护

  • 你能用纯锈来表示你想要的功能吗
  • 现有语法是否过于冗长
  • 这是宏的候选者

    macro_rules! device_test {
        (no-device, $name:ident, {$($body:tt)+}) => (
            #[test]
            fn $name() {
                $($body)+
            }
        );
        (device1, $name:ident, {$($body:tt)+}) => (
            #[test]
            #[cfg_attr(not(feature = "test-type-one"), ignore)]
            fn $name() {
                $($body)+
            }
        );
        (device2, $name:ident, {$($body:tt)+}) => (
            #[test]
            #[cfg_attr(not(feature = "test-type-two"), ignore)]
            fn $name() {
                $($body)+
            }
        );
    }
    
    device_test!(no-device, one, {
        assert_eq!(2, 1+1)
    });
    
    device_test!(device1, two, {
        assert_eq!(3, 1+1)
    });
    
    类型2的功能是类型1功能的超集

    在要素定义中反映这一点以简化代码:

    [features]
    test1 = []
    test2 = ["test1"]
    
    如果这样做,您不需要在配置属性中包含
    any
    all

    默认功能
    不测试设备

    这似乎没有用;而是使用由正常测试配置保护的正常测试:

    #[cfg(test)]
    mod test {
        #[test]
        fn no_device_needed() {}
    }
    
    如果遵循此操作,则可以从宏中删除此案例



    我认为,如果您遵循这两个建议,您甚至不需要宏。

    您可以尝试注释一个完整的mod块,而不是单个mod块functions@hellow但这只在我想禁用编译时有效,而不是因为忽略测试用例,对吗?