Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to run unit tests using KUnit with a user-space like syntax #950

Open
wants to merge 3 commits into
base: rust
Choose a base branch
from

Commits on Mar 19, 2023

  1. rust: kunit: add KUnit case and suite macros

    Add a couple of Rust macros to allow to develop KUnit tests without
    relying on generated C code:
    
     - The `kunit_unsafe_test_suite!` Rust macro is similar to the
       `kunit_test_suite` C macro.
     - The `kunit_case!` Rust macro is similar to the `KUNIT_CASE` C macro.
       It can be used with parameters to generate a test case or without
       parameters to be used as delimiter in `kunit_test_suite!`.
    
    While these macros can be used on their own, a future patch will
    introduce another macro to create KUnit tests using a user-space like
    syntax.
    
    Co-developed-by: David Gow <davidgow@google.com>
    Signed-off-by: David Gow <davidgow@google.com>
    Signed-off-by: José Expósito <jose.exposito89@gmail.com>
    JoseExposito committed Mar 19, 2023
    Configuration menu
    Copy the full SHA
    fa4f936 View commit details
    Browse the repository at this point in the history
  2. rust: macros: add macro to easily run KUnit tests

    Add a new procedural macro (`#[kunit_tests(kunit_test_suit_name)]`) to
    run KUnit tests using a user-space like syntax.
    
    The macro, that should be used on modules, transforms every `#[test]`
    in a `kunit_case!` and adds a `kunit_unsafe_test_suite!` registering
    all of them.
    
    The only difference with user-space tests is that instead of using
    `#[cfg(test)]`, `#[kunit_tests(kunit_test_suit_name)]` is used.
    
    Note that `#[cfg(CONFIG_KUNIT)]` is added so the test module is not
    compiled when `CONFIG_KUNIT` is set to `n`.
    
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: José Expósito <jose.exposito89@gmail.com>
    JoseExposito committed Mar 19, 2023
    Configuration menu
    Copy the full SHA
    587d3df View commit details
    Browse the repository at this point in the history
  3. rust: kunit: allow to know if we are in a test

    In some cases, you need to call test-only code from outside the test
    case, for example, to mock a function or a module.
    
    In order to check whether we are in a test or not, we need to test if
    `CONFIG_KUNIT` is set.
    Unfortunately, we cannot rely only on this condition because some
    distros compile KUnit in production kernels, so checking at runtime
    that `current->kunit_test != NULL` is required.
    
    Note that the C function `kunit_get_current_test()` can not be used
    because it is not present in the current Rust tree yet. Once it is
    available we might want to change our Rust wrapper to use it.
    
    This patch adds a function to know whether we are in a KUnit test or
    not and examples showing how to mock a function and a module.
    
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: José Expósito <jose.exposito89@gmail.com>
    JoseExposito committed Mar 19, 2023
    Configuration menu
    Copy the full SHA
    c393008 View commit details
    Browse the repository at this point in the history