11 - tests Flashcards

1
Q

How to make sure that a test function panics with a specific panic message?

A
add:
#[should_panic(expected = "panic because...")]
before function signature.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to make a test fail if it returns an Err(error)?

A
Add return type to the function signature like so:
#[test]
fn test_func() -> Result

*You can change String to the desire error type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

By default, how many threads would run by calling “cargo test”?

A

By default, any test run in parallel, each test runs in its own thread.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can we run test serially?

A

We can run test serially with the following command:
cargo test – –test-threads=1

*test-threads is an argument given to the resulting binary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to show stdout output of a test function?

A

run:

cargo test – –show-output

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you run a specific test? how can you run a specific test module?

A

run:
cargo test test_function_name

cargo test test_module_name::

*you can also specify part of the name, and all the function that match the pattern will be tested

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you ignore a specific test? how can you run only the ignored tests?

A
to ignore a specific test - add above its signature:
#[ignore]

to test all ignored tests, run:
cargo test – –ignored

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When will a test module code be compiled? will cargo build do it?

A

Cargo will only compile the test code under #[cfg(test)] when we run cargo test.
Cargo build won’t do it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why there is no need to make internal_adder public for the test modules?

fn internal_adder(a: i32, b: i32) -> i32 {
    a + b
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn internal() {
        assert_eq!(4, internal_adder(2, 2));
    }
}
A
Because in Rust, because child module are able to access anything in their parent module, even private fields. 
(in the example code, tests module is the child)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the convention of the location of unit tests in rust?

A

The convention is that unit test go in the same files as the code you’re testing.

*We use [cfg(test)] to specify that they shouldn’t be included in the compiled result

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Where does integration tests are written in Rust?

A

In Rust, integration tests are entirely external to your library.
We create a tests directory at the top level of our project directory, next to src. Cargo knows to look for integration test files in this directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Do integration tests need the #[cfg(test)] annotation?

A

No, because of their known location. Cargo knows to look for integration test files in the “tests” directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does cargo do with the files in the tests directory?

A

Cargo will compile each of the files as an individual crate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The following code won’t compile. Why?

in tests/integration_test.rs:
use adder;
#[test]
fn it_adds_two() {
    assert_eq!(4, adder::add_two(2));
}

in sec/lib.rs:
fn add_two(a: i32) -> i32 {
a + 2
}

A

because add_two must be public. In tests directory we test only the public API, and can’t access private functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to run only integration tests?

A

cargo test –test –integration_test

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

We want to add setup function before our integration step. This might be a useful step in multiple integration test files.
What is the convention to do it?

A

In test create a directory named “common”, and in it create a file named “mod.rs”.
In this file we can create a function:

pub fn setup() {
  // setup code specific to my library's tests...
}

In any of the integration test files we can use the setup in the following way:

mod common;

#[test]
fn some_test() {
  common::setup();
  // continue test logic here...
}