(window.webpackJsonp=window.webpackJsonp||[]).push([[20],{498:function(t,e,s){"use strict";s.r(e);var a=s(4),n=Object(a.a)({},(function(){var t=this,e=t.$createElement,s=t._self._c||e;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"测试"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#测试"}},[t._v("#")]),t._v(" 测试")]),t._v(" "),s("p",[t._v("我们知道测试是任何软件不可缺少的一部分！Rust 对单元和集成测试提供一流的支持（参见《Rust 程序设计语言》中的关于"),s("a",{attrs:{href:"https://doc.rust-lang.org/book/ch11-00-testing.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("测试的章节"),s("OutboundLink")],1),t._v("）。")]),t._v(" "),s("p",[t._v("通过上面链接的关于测试章节，我们看到了如何编写单元测试和集成测试。在代码目录组织上，我们可以将单元测试放在需要测试的模块中，并将集成测试放在源码中 "),s("code",[t._v("tests/")]),t._v(" 目录中：")]),t._v(" "),s("div",{staticClass:"language-txt extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v("foo\n├── Cargo.toml\n├── src\n│   └── main.rs\n└── tests\n    ├── my_test.rs\n    └── my_other_test.rs\n")])])]),s("p",[s("code",[t._v("tests")]),t._v(" 目录下的每个文件都是一个单独的集成测试。")]),t._v(" "),s("p",[s("code",[t._v("cargo")]),t._v(" 很自然地提供了一种便捷的方法来运行所有测试！")]),t._v(" "),s("div",{staticClass:"language-sh extra-class"},[s("pre",{pre:!0,attrs:{class:"language-sh"}},[s("code",[t._v("cargo "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("test")]),t._v("\n")])])]),s("p",[t._v("你将会看到像这样的输出：")]),t._v(" "),s("div",{staticClass:"language-txt extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v("$ cargo test\n   Compiling blah v0.1.0 (file:///nobackup/blah)\n    Finished dev [unoptimized + debuginfo] target(s) in 0.89 secs\n     Running target/debug/deps/blah-d3b32b97275ec472\n\nrunning 3 tests\ntest test_bar ... ok\ntest test_baz ... ok\ntest test_foo_bar ... ok\ntest test_foo ... ok\n\ntest result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out\n")])])]),s("p",[t._v("你还可以运行如下测试，其中名称匹配一个模式：")]),t._v(" "),s("div",{staticClass:"language-sh extra-class"},[s("pre",{pre:!0,attrs:{class:"language-sh"}},[s("code",[t._v("cargo "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("test")]),t._v(" test_foo\n")])])]),s("div",{staticClass:"language-txt extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v("$ cargo test test_foo\n   Compiling blah v0.1.0 (file:///nobackup/blah)\n    Finished dev [unoptimized + debuginfo] target(s) in 0.35 secs\n     Running target/debug/deps/blah-d3b32b97275ec472\n\nrunning 2 tests\ntest test_foo ... ok\ntest test_foo_bar ... ok\n\ntest result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out\n")])])]),s("p",[t._v("需要注意的一点是："),s("code",[t._v("cargo")]),t._v(" 可能同时进行多项测试，因此请确保它们不会相互竞争。例如，如果它们都输出到文件，则应该将它们写入不同的文件。")])])}),[],!1,null,null,null);e.default=n.exports}}]);