mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-01-23 23:50:25 +08:00
Adding some listing numbers
This commit is contained in:
parent
e3554b774b
commit
d242dc44a7
@ -229,7 +229,7 @@ is useful when we want to test that calling a particular function will cause an
|
|||||||
error. For example, let's test something that we know will panic from Chapter
|
error. For example, let's test something that we know will panic from Chapter
|
||||||
8: attempting to create a slice using range syntax with byte indices that
|
8: attempting to create a slice using range syntax with byte indices that
|
||||||
aren't on character boundaries. Add the `#[should_panic]` attribute before the
|
aren't on character boundaries. Add the `#[should_panic]` attribute before the
|
||||||
function like the `#[test]` attribute:
|
function like the `#[test]` attribute, as shown in Listing 11-1:
|
||||||
|
|
||||||
Filename: src/lib.rs
|
Filename: src/lib.rs
|
||||||
|
|
||||||
@ -242,6 +242,10 @@ fn slice_not_on_char_boundaries() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
<caption>
|
||||||
|
Listing 11-1: A test expecting a `panic!`
|
||||||
|
</caption>
|
||||||
|
|
||||||
This test will succeed, since the code panics and we said that it should. If
|
This test will succeed, since the code panics and we said that it should. If
|
||||||
this code happened to run and did not cause a `panic!`, this test would fail.
|
this code happened to run and did not cause a `panic!`, this test would fail.
|
||||||
|
|
||||||
@ -249,7 +253,8 @@ this code happened to run and did not cause a `panic!`, this test would fail.
|
|||||||
didn't fail for a different reason than the one you were expecting. To help
|
didn't fail for a different reason than the one you were expecting. To help
|
||||||
with this, an optional `expected` parameter can be added to the `should_panic`
|
with this, an optional `expected` parameter can be added to the `should_panic`
|
||||||
attribute. The test harness will make sure that the failure message contains
|
attribute. The test harness will make sure that the failure message contains
|
||||||
the provided text. A safer version of the example above would be:
|
the provided text. A safer version of Listing 11-1 would be the following, in
|
||||||
|
Listing 11-2:
|
||||||
|
|
||||||
Filename: src/lib.rs
|
Filename: src/lib.rs
|
||||||
|
|
||||||
@ -262,6 +267,12 @@ fn slice_not_on_char_boundaries() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
<!-- I will add ghosting in libreoffice /Carol -->
|
||||||
|
|
||||||
|
<caption>
|
||||||
|
Listing 11-2: A test expecting a `panic!` with a particular message
|
||||||
|
</caption>
|
||||||
|
|
||||||
Try on your own to see what happens when a `should_panic` test panics but
|
Try on your own to see what happens when a `should_panic` test panics but
|
||||||
doesn't match the expected message: cause a `panic!` that happens for a
|
doesn't match the expected message: cause a `panic!` that happens for a
|
||||||
different reason in this test, or change the expected panic message to
|
different reason in this test, or change the expected panic message to
|
||||||
|
@ -48,7 +48,7 @@ working on code in a particular area, you might want to only run the tests
|
|||||||
having to do with that code. `cargo test` takes an argument that allows you to
|
having to do with that code. `cargo test` takes an argument that allows you to
|
||||||
only run certain tests, specified by name.
|
only run certain tests, specified by name.
|
||||||
|
|
||||||
Let's create three tests with the following names:
|
Let's create three tests with the following names as shown in Listing 11-3:
|
||||||
|
|
||||||
Filename: src/lib.rs
|
Filename: src/lib.rs
|
||||||
|
|
||||||
@ -69,6 +69,10 @@ fn one_hundred() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
<caption>
|
||||||
|
Listing 11-3: Three tests with a variety of names
|
||||||
|
</caption>
|
||||||
|
|
||||||
Running with different arguments will run different subsets of the tests. No
|
Running with different arguments will run different subsets of the tests. No
|
||||||
arguments, as we've already seen, runs all the tests:
|
arguments, as we've already seen, runs all the tests:
|
||||||
|
|
||||||
@ -115,7 +119,7 @@ test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured
|
|||||||
Module names become part of the test name, so module names can be used in a
|
Module names become part of the test name, so module names can be used in a
|
||||||
similar way to run just the tests for a particular module. For example, if our
|
similar way to run just the tests for a particular module. For example, if our
|
||||||
code was organized into a module named `adding` and a module named
|
code was organized into a module named `adding` and a module named
|
||||||
`subtracting` with tests in each:
|
`subtracting` with tests in each, as in Listing 11-4:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
mod adding {
|
mod adding {
|
||||||
@ -143,6 +147,10 @@ mod subtracting {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
<caption>
|
||||||
|
Listing 11-4: Tests in two modules named `adding` and `subtracting`
|
||||||
|
</caption>
|
||||||
|
|
||||||
Running `cargo test` will run all of the tests, and the module names will
|
Running `cargo test` will run all of the tests, and the module names will
|
||||||
appear in the test names in the output:
|
appear in the test names in the output:
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ this is a common use of globs.
|
|||||||
Up until now in this chapter, we've been writing tests in our `adder` project
|
Up until now in this chapter, we've been writing tests in our `adder` project
|
||||||
that don't actually call any code we've written. Let's change that now! In
|
that don't actually call any code we've written. Let's change that now! In
|
||||||
*src/lib.rs*, place this `add_two` function and `tests` module that has a test
|
*src/lib.rs*, place this `add_two` function and `tests` module that has a test
|
||||||
function to exercise the code:
|
function to exercise the code, as shown in Listing 11-5:
|
||||||
|
|
||||||
Filename: src/lib.rs
|
Filename: src/lib.rs
|
||||||
|
|
||||||
@ -81,6 +81,10 @@ mod tests {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
<caption>
|
||||||
|
Listing 11-5: Testing the function `add_two` in a child `tests` module
|
||||||
|
</caption>
|
||||||
|
|
||||||
Notice in addition to the test function, we also added `use add_two;` within
|
Notice in addition to the test function, we also added `use add_two;` within
|
||||||
the `tests` module. This brings the code we want to test into the scope of the
|
the `tests` module. This brings the code we want to test into the scope of the
|
||||||
inner `tests` module, just like we'd need to do for any inner module. If we run
|
inner `tests` module, just like we'd need to do for any inner module. If we run
|
||||||
@ -115,8 +119,8 @@ everything into the `test` module scope at once.
|
|||||||
There's controversy within the testing community about whether you should write
|
There's controversy within the testing community about whether you should write
|
||||||
unit tests for private functions or not. Regardless of which testing ideology
|
unit tests for private functions or not. Regardless of which testing ideology
|
||||||
you adhere to, Rust does allow you to test private functions due to the way
|
you adhere to, Rust does allow you to test private functions due to the way
|
||||||
that the privacy rules work. Consider this code with the private function
|
that the privacy rules work. Consider the code in Listing 11-6 with the private
|
||||||
`internal_adder`:
|
function `internal_adder`:
|
||||||
|
|
||||||
Filename: src/lib.rs
|
Filename: src/lib.rs
|
||||||
|
|
||||||
@ -140,6 +144,10 @@ mod tests {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
<caption>
|
||||||
|
Listing 11-6: Testing a private function
|
||||||
|
</caption>
|
||||||
|
|
||||||
Because tests are just Rust code and the `tests` module is just another module,
|
Because tests are just Rust code and the `tests` module is just another module,
|
||||||
we can import and call `internal_adder` in a test just fine. If you don't think
|
we can import and call `internal_adder` in a test just fine. If you don't think
|
||||||
private functions should be tested, there's nothing in Rust that will compel
|
private functions should be tested, there's nothing in Rust that will compel
|
||||||
@ -159,9 +167,9 @@ Cargo has support for integration tests in the *tests* directory. If you make
|
|||||||
one and put Rust files inside, Cargo will compile each of the files as an
|
one and put Rust files inside, Cargo will compile each of the files as an
|
||||||
individual crate. Let's give it a try!
|
individual crate. Let's give it a try!
|
||||||
|
|
||||||
First, make a *tests* directory at the top level of your project directory, next
|
First, make a *tests* directory at the top level of your project directory,
|
||||||
to *src*. Then, make a new file, *tests/integration_test.rs*, and put this
|
next to *src*. Then, make a new file, *tests/integration_test.rs*, and put the
|
||||||
inside:
|
code in Listing 11-7 inside:
|
||||||
|
|
||||||
Filename: tests/integration_test.rs
|
Filename: tests/integration_test.rs
|
||||||
|
|
||||||
@ -174,6 +182,10 @@ fn it_adds_two() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
<caption>
|
||||||
|
Listing 11-7: An integration test of a function in the `adder` crate
|
||||||
|
</caption>
|
||||||
|
|
||||||
We now have `extern crate adder` at the top, which we didn't need in the unit
|
We now have `extern crate adder` at the top, which we didn't need in the unit
|
||||||
tests. Each test in the `tests` directory is an entirely separate crate, so we
|
tests. Each test in the `tests` directory is an entirely separate crate, so we
|
||||||
need to import our library into each of them. This is also why `tests` is a
|
need to import our library into each of them. This is also why `tests` is a
|
||||||
|
Loading…
Reference in New Issue
Block a user