@@ -2921,15 +2921,11 @@ it `false`, so this test should fail. Let's try it!
2921
2921
``` {notrust,ignore}
2922
2922
$ cargo test
2923
2923
Compiling testing v0.0.1 (file:///home/you/projects/testing)
2924
- /home/you/projects/testing/src/main.rs:1:1: 3:2 warning: code is never used: `main`, #[warn(dead_code)] on by default
2924
+ /home/you/projects/testing/src/main.rs:1:1: 3:2 warning: function is never used: `main`, #[warn(dead_code)] on by default
2925
2925
/home/you/projects/testing/src/main.rs:1 fn main() {
2926
2926
/home/you/projects/testing/src/main.rs:2 println!("Hello, world!")
2927
2927
/home/you/projects/testing/src/main.rs:3 }
2928
-
2929
- running 0 tests
2930
-
2931
- test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
2932
-
2928
+ Running target/lib-654ce120f310a3a5
2933
2929
2934
2930
running 1 test
2935
2931
test foo ... FAILED
@@ -2946,7 +2942,7 @@ failures:
2946
2942
2947
2943
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
2948
2944
2949
- task '<main>' failed at 'Some tests failed', /home/you/src/rust/src/libtest/lib.rs:242
2945
+ task '<main>' failed at 'Some tests failed', /home/you/src/rust/src/libtest/lib.rs:243
2950
2946
```
2951
2947
2952
2948
Lots of output! Let's break this down:
@@ -2960,7 +2956,7 @@ You can run all of your tests with `cargo test`. This runs both your tests in
2960
2956
` tests ` , as well as the tests you put inside of your crate.
2961
2957
2962
2958
``` {notrust,ignore}
2963
- /home/you/projects/testing/src/main.rs:1:1: 3:2 warning: code is never used: `main`, #[warn(dead_code)] on by default
2959
+ /home/you/projects/testing/src/main.rs:1:1: 3:2 warning: function is never used: `main`, #[warn(dead_code)] on by default
2964
2960
/home/you/projects/testing/src/main.rs:1 fn main() {
2965
2961
/home/you/projects/testing/src/main.rs:2 println!("Hello, world!")
2966
2962
/home/you/projects/testing/src/main.rs:3 }
@@ -2974,18 +2970,8 @@ We'll turn this lint off for just this function soon. For now, just ignore this
2974
2970
output.
2975
2971
2976
2972
``` {notrust,ignore}
2977
- running 0 tests
2978
-
2979
- test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
2980
- ```
2973
+ Running target/lib-654ce120f310a3a5
2981
2974
2982
- Wait a minute, zero tests? Didn't we define one? Yup. This output is from
2983
- attempting to run the tests in our crate, of which we don't have any.
2984
- You'll note that Rust reports on several kinds of tests: passed, failed,
2985
- ignored, and measured. The 'measured' tests refer to benchmark tests, which
2986
- we'll cover soon enough!
2987
-
2988
- ``` {notrust,ignore}
2989
2975
running 1 test
2990
2976
test foo ... FAILED
2991
2977
```
@@ -3008,7 +2994,7 @@ failures:
3008
2994
3009
2995
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
3010
2996
3011
- task '<main>' failed at 'Some tests failed', /home/you/src/rust/src/libtest/lib.rs:242
2997
+ task '<main>' failed at 'Some tests failed', /home/you/src/rust/src/libtest/lib.rs:243
3012
2998
```
3013
2999
3014
3000
After all the tests run, Rust will show us any output from our failed tests.
@@ -3029,24 +3015,25 @@ And then try to run our tests again:
3029
3015
``` {notrust,ignore}
3030
3016
$ cargo test
3031
3017
Compiling testing v0.0.1 (file:///home/you/projects/testing)
3032
- /home/you/projects/testing/src/main.rs:1:1: 3:2 warning: code is never used: `main`, #[warn(dead_code)] on by default
3033
- /home/you/projects/testing/src/main.rs:1 fn main() {
3034
- /home/you/projects/testing/src/main.rs:2 println!("Hello, world");
3035
- /home/you/projects/testing/src/main.rs:3 }
3036
-
3037
- running 0 tests
3038
-
3039
- test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
3040
-
3018
+ Running target/lib-654ce120f310a3a5
3041
3019
3042
3020
running 1 test
3043
3021
test foo ... ok
3044
3022
3045
3023
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
3024
+
3025
+ Running target/testing-6d7518593c7c3ee5
3026
+
3027
+ running 0 tests
3028
+
3029
+ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
3046
3030
```
3047
3031
3048
- Nice! Our test passes, as we expected. Let's get rid of that warning for our ` main `
3049
- function. Change your ` src/main.rs ` to look like this:
3032
+ Nice! Our test passes, as we expected. Note how we didn't get the
3033
+ ` main ` warning this time? This is because ` src/main.rs ` didn't
3034
+ need recompiling, but we'll get that warning again if we
3035
+ change (and recompile) that file. Let's get rid of that
3036
+ warning; change your ` src/main.rs ` to look like this:
3050
3037
3051
3038
``` {rust}
3052
3039
#[cfg(not(test))]
@@ -3062,21 +3049,24 @@ our tests, it sets things up so that `cfg(test)` is true. But we want to only
3062
3049
include ` main ` when it's _ not_ true. So we use ` not ` to negate things:
3063
3050
` cfg(not(test)) ` will only compile our code when the ` cfg(test) ` is false.
3064
3051
3065
- With this attribute, we won't get the warning:
3052
+ With this attribute we won't get the warning (even
3053
+ though ` src/main.rs ` gets recompiled this time):
3066
3054
3067
3055
``` {notrust,ignore}
3068
3056
$ cargo test
3069
3057
Compiling testing v0.0.1 (file:///home/you/projects/testing)
3070
-
3071
- running 0 tests
3072
-
3073
- test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
3074
-
3058
+ Running target/lib-654ce120f310a3a5
3075
3059
3076
3060
running 1 test
3077
3061
test foo ... ok
3078
3062
3079
3063
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
3064
+
3065
+ Running target/testing-6d7518593c7c3ee5
3066
+
3067
+ running 0 tests
3068
+
3069
+ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
3080
3070
```
3081
3071
3082
3072
Nice. Okay, let's write a real test now. Change your ` tests/lib.rs `
@@ -3156,21 +3146,30 @@ Let's give it a run:
3156
3146
``` {ignore,notrust}
3157
3147
$ cargo test
3158
3148
Compiling testing v0.0.1 (file:///home/you/projects/testing)
3149
+ Running target/lib-654ce120f310a3a5
3150
+
3151
+ running 1 test
3152
+ test math_checks_out ... ok
3153
+
3154
+ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
3155
+
3156
+ Running target/testing-6d7518593c7c3ee5
3159
3157
3160
3158
running 0 tests
3161
3159
3162
3160
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
3163
3161
3162
+ Running target/testing-8a94b31f7fd2e8fe
3164
3163
3165
3164
running 0 tests
3166
3165
3167
3166
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
3168
3167
3168
+ Doc-tests testing
3169
3169
3170
- running 1 test
3171
- test math_checks_out ... ok
3170
+ running 0 tests
3172
3171
3173
- test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
3172
+ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
3174
3173
```
3175
3174
3176
3175
Great! One test passed. We've got an integration test showing that our public
@@ -3196,21 +3195,30 @@ If you run `cargo test`, you should get the same output:
3196
3195
``` {ignore,notrust}
3197
3196
$ cargo test
3198
3197
Compiling testing v0.0.1 (file:///home/you/projects/testing)
3198
+ Running target/lib-654ce120f310a3a5
3199
+
3200
+ running 1 test
3201
+ test math_checks_out ... ok
3202
+
3203
+ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
3204
+
3205
+ Running target/testing-6d7518593c7c3ee5
3199
3206
3200
3207
running 0 tests
3201
3208
3202
3209
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
3203
3210
3211
+ Running target/testing-8a94b31f7fd2e8fe
3204
3212
3205
3213
running 0 tests
3206
3214
3207
3215
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
3208
3216
3217
+ Doc-tests testing
3209
3218
3210
- running 1 test
3211
- test math_checks_out ... ok
3219
+ running 0 tests
3212
3220
3213
- test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
3221
+ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
3214
3222
```
3215
3223
3216
3224
If we tried to write a test for these two new functions, it wouldn't
@@ -3283,23 +3291,32 @@ Let's give it a shot:
3283
3291
``` {ignore,notrust}
3284
3292
$ cargo test
3285
3293
Compiling testing v0.0.1 (file:///home/you/projects/testing)
3294
+ Running target/lib-654ce120f310a3a5
3295
+
3296
+ running 1 test
3297
+ test math_checks_out ... ok
3298
+
3299
+ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
3300
+
3301
+ Running target/testing-6d7518593c7c3ee5
3302
+
3303
+ running 0 tests
3304
+
3305
+ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
3306
+
3307
+ Running target/testing-8a94b31f7fd2e8fe
3286
3308
3287
3309
running 2 tests
3288
3310
test test::test_times_four ... ok
3289
3311
test test::test_add_three ... ok
3290
3312
3291
3313
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured
3292
3314
3315
+ Doc-tests testing
3293
3316
3294
3317
running 0 tests
3295
3318
3296
3319
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
3297
-
3298
-
3299
- running 1 test
3300
- test math_checks_out ... ok
3301
-
3302
- test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
3303
3320
```
3304
3321
3305
3322
Cool! We now have two tests of our internal functions. You'll note that there
0 commit comments