Attribute Macro nitrokey_test::test[][src]

#[test]

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 any Nitrokey device, but leave the device connection to the user and just provide the model:

#[nitrokey_test::test]
fn some_other_nitrokey_test(model: nitrokey::Model) {
  // Connect to a device of the provided model.
}

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 Pro device, but leave the device connection to the user:

#[nitrokey_test::test(pro)]
fn some_other_pro_test() {
  // Do something on a Pro device.
}

A model can be provided optionally, like so:

#[nitrokey_test::test(pro)]
fn some_other_pro_test(model: nitrokey::Model) {
  assert_eq!(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());
}