Attribute Macro nitrokey_test::test

source ·
#[test]
Expand description

A procedural macro for the test attribute.

The attribute can be used to define a test that accepts a Nitrokey device object (which can be any of nitrokey::Pro, nitrokey::Storage, or nitrokey::DeviceWrapper), and runs a test against that device. If the device type was specified as nitrokey::DeviceWrapper, the test will actually be invoked for a Nitrokey Pro as well as a Nitrokey Storage. Irrespective, the test is skipped if the device cannot be found. It also supports running tests when no device is present, which is required for tasks such as handling of error conditions. The test function must not accept a device object in that case (i.e., have no parameters).

Example

Test functionality on an arbitrary Nitrokey device (i.e., Pro or Storage):

// Note that no test would actually run, regardless of `no_run`,
// because we do not invoke the function.
#[nitrokey_test::test]
fn some_nitrokey_test(device: nitrokey::DeviceWrapper) {
  assert_eq!(device.get_serial_number().unwrap().len(), 8);
}

Test functionality on a Nitrokey Pro device:

#[nitrokey_test::test]
fn some_pro_test(device: nitrokey::Pro) {
  assert_eq!(device.get_model(), nitrokey::Model::Pro);
}

Test functionality on a Nitrokey Storage device:

#[nitrokey_test::test]
fn some_storage_test(device: nitrokey::Storage) {
  assert_eq!(device.get_model(), nitrokey::Model::Storage);
}

Test functionality when no device is present:

#[nitrokey_test::test]
fn no_device() {
  assert!(nitrokey::connect().is_err());
}