Rust入门教程(八).html 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <!DOCTYPE html>
  2. <html lang="en-US">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <title>Rust入门教程(八):编写和运行测试 | Rust训练营教程文档</title>
  7. <meta name="generator" content="VuePress 1.9.10">
  8. <link rel="icon" href="/rust_camp_tutorial/logo.png">
  9. <meta name="description" content="DragonOS-Rust camp">
  10. <link rel="preload" href="/rust_camp_tutorial/assets/css/0.styles.7dd9be3e.css" as="style"><link rel="preload" href="/rust_camp_tutorial/assets/js/app.d7ab8f65.js" as="script"><link rel="preload" href="/rust_camp_tutorial/assets/js/2.3dc1b8de.js" as="script"><link rel="preload" href="/rust_camp_tutorial/assets/js/1.7f771cfb.js" as="script"><link rel="preload" href="/rust_camp_tutorial/assets/js/30.72f41aed.js" as="script"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/10.23a1f579.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/11.c389195a.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/12.1d996921.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/13.4d4410c4.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/14.37ef2a72.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/15.5542c093.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/16.d48fd1ce.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/17.bd8d538c.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/18.6d3b94c1.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/19.eb35cfee.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/20.c11ec329.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/21.db1b5d88.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/22.7714be7b.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/23.7f3a9620.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/24.2f88b37d.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/25.df99dd5f.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/26.606cfbc8.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/27.928f1e6b.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/28.f61a69ee.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/29.802642cf.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/3.5322f14a.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/31.e1aa8cbc.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/32.30c1ff3b.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/33.f21667a4.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/4.84e1e480.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/5.f0541060.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/6.dfb06aa0.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/7.7551a9fb.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/vendors~docsearch.5e19b665.js">
  11. <link rel="stylesheet" href="/rust_camp_tutorial/assets/css/0.styles.7dd9be3e.css">
  12. </head>
  13. <body>
  14. <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/rust_camp_tutorial/" class="home-link router-link-active"><img src="logo.png" alt="Rust训练营教程文档" class="logo"> <span class="site-name can-hide">Rust训练营教程文档</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/rust_camp_tutorial/" class="nav-link">
  15. 首页
  16. </a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/rust_camp_tutorial/" class="nav-link">
  17. 首页
  18. </a></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Rust入门教程(八):编写和运行测试</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/rust_camp_tutorial/Rust%E6%96%87%E6%A1%A3/Rust%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B%EF%BC%88%E5%85%AB%EF%BC%89.html#一、测试的使用及演示" class="sidebar-link">一、测试的使用及演示</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/rust_camp_tutorial/Rust%E6%96%87%E6%A1%A3/Rust%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B%EF%BC%88%E5%85%AB%EF%BC%89.html#_1-1-测试演示" class="sidebar-link">1.1 测试演示</a></li><li class="sidebar-sub-header"><a href="/rust_camp_tutorial/Rust%E6%96%87%E6%A1%A3/Rust%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B%EF%BC%88%E5%85%AB%EF%BC%89.html#_1-2-测试失败" class="sidebar-link">1.2 测试失败</a></li></ul></li><li><a href="/rust_camp_tutorial/Rust%E6%96%87%E6%A1%A3/Rust%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B%EF%BC%88%E5%85%AB%EF%BC%89.html#二、断言" class="sidebar-link">二、断言</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/rust_camp_tutorial/Rust%E6%96%87%E6%A1%A3/Rust%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B%EF%BC%88%E5%85%AB%EF%BC%89.html#_2-1-使用-assert-宏检查测试结果" class="sidebar-link">2.1 使用 assert! 宏检查测试结果</a></li><li class="sidebar-sub-header"><a href="/rust_camp_tutorial/Rust%E6%96%87%E6%A1%A3/Rust%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B%EF%BC%88%E5%85%AB%EF%BC%89.html#_2-2-使用-assert-eq-和-assert-ne-测试相等性" class="sidebar-link">2.2 使用 asserteq! 和 assertne! 测试相等性</a></li></ul></li><li><a href="/rust_camp_tutorial/Rust%E6%96%87%E6%A1%A3/Rust%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B%EF%BC%88%E5%85%AB%EF%BC%89.html#三、自定义错误消息" class="sidebar-link">三、自定义错误消息</a><ul class="sidebar-sub-headers"></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="rust入门教程-八-编写和运行测试"><a href="#rust入门教程-八-编写和运行测试" class="header-anchor">#</a> Rust入门教程(八):编写和运行测试</h1> <blockquote><p>这一章主要介绍 Rust 的测试。在 Rust 中,一个测试实际上就是一个函数,用于验证非测试代码的功能是否和预期一致。</p></blockquote> <blockquote><p>测试函数体通常执行下面三个操作,也称 <code>3a</code> 操作:</p> <ol><li>准备数据或状态(arrange);</li> <li>运行被测试的代码(act);</li> <li>断言结果(assert)。</li></ol></blockquote> <h2 id="一、测试的使用及演示"><a href="#一、测试的使用及演示" class="header-anchor">#</a> 一、测试的使用及演示</h2> <ul><li>测试函数需要使用 <code>test</code> 属性(attribute)进行标注
  19. <ul><li>attribute 就是一段代码的元数据</li> <li>在函数紧接着上面一行添加 <code>#[test]</code>,就将函数变成测试函数了</li></ul></li> <li>运行测试
  20. <ul><li>使用 <code>cargo test</code> 命令运行所有测试
  21. <ul><li>Rust 会构建一个 Test Runner 可执行文件,会运行标注的 test 函数,并报告运行是否成功</li></ul></li> <li>当使用 cargo 创建 library 项目时,会生成一个 test module,里面有默认的 test 函数
  22. <ul><li>可以添加任意数量的 test module 和 test 函数</li></ul></li></ul></li></ul> <h3 id="_1-1-测试演示"><a href="#_1-1-测试演示" class="header-anchor">#</a> 1.1 测试演示</h3> <p>我们输入命令 <code>cargo new test_demo --lib</code> 创建一个项目,在 <code>lib.rs</code> 文件中看到这样的函数:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token attribute attr-name">#[cfg(test)]</span>
  23. <span class="token keyword">mod</span> <span class="token module-declaration namespace">tests</span> <span class="token punctuation">{</span>
  24. <span class="token attribute attr-name">#[test]</span>
  25. <span class="token keyword">fn</span> <span class="token function-definition function">it_works</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  26. <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">;</span>
  27. <span class="token macro property">assert_eq!</span><span class="token punctuation">(</span>result<span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  28. <span class="token punctuation">}</span>
  29. <span class="token punctuation">}</span>
  30. </code></pre></div><p>这里 <code>assert_eq!</code> 是一个断言的宏,判断两个数是否相等。</p> <p>然后运行 <code>cargo test</code>,结果如下:</p> <div class="language-rust extra-class"><pre class="language-rust"><code>➜ ~<span class="token operator">/</span>code<span class="token operator">/</span>rust<span class="token operator">/</span>test_demo git<span class="token punctuation">:</span><span class="token punctuation">(</span>master<span class="token punctuation">)</span> ✗ cargo test
  31. <span class="token class-name">Compiling</span> test_demo v0<span class="token number">.1</span><span class="token punctuation">.</span><span class="token number">0</span> <span class="token punctuation">(</span><span class="token operator">/</span>home<span class="token operator">/</span>cherry<span class="token operator">/</span>code<span class="token operator">/</span>rust<span class="token operator">/</span>test_demo<span class="token punctuation">)</span>
  32. <span class="token class-name">Finished</span> test <span class="token punctuation">[</span>unoptimized <span class="token operator">+</span> debuginfo<span class="token punctuation">]</span> <span class="token function">target</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token number">2</span><span class="token punctuation">.</span>33s
  33. <span class="token class-name">Running</span> <span class="token function">unittests</span> <span class="token punctuation">(</span>target<span class="token operator">/</span>debug<span class="token operator">/</span>deps<span class="token operator">/</span>test_demo<span class="token operator">-</span>357c557c333f0e0d<span class="token punctuation">)</span>
  34. running <span class="token number">1</span> test
  35. test <span class="token namespace">tests<span class="token punctuation">::</span></span>it_works <span class="token punctuation">...</span> ok
  36. test result<span class="token punctuation">:</span> ok<span class="token punctuation">.</span> <span class="token number">1</span> passed<span class="token punctuation">;</span> <span class="token number">0</span> failed<span class="token punctuation">;</span> <span class="token number">0</span> ignored<span class="token punctuation">;</span> <span class="token number">0</span> measured<span class="token punctuation">;</span> <span class="token number">0</span> filtered out<span class="token punctuation">;</span> finished <span class="token keyword">in</span> <span class="token number">0</span><span class="token punctuation">.</span>00s
  37. <span class="token class-name">Doc</span><span class="token operator">-</span>tests test_demo
  38. running <span class="token number">0</span> tests
  39. test result<span class="token punctuation">:</span> ok<span class="token punctuation">.</span> <span class="token number">0</span> passed<span class="token punctuation">;</span> <span class="token number">0</span> failed<span class="token punctuation">;</span> <span class="token number">0</span> ignored<span class="token punctuation">;</span> <span class="token number">0</span> measured<span class="token punctuation">;</span> <span class="token number">0</span> filtered out<span class="token punctuation">;</span> finished <span class="token keyword">in</span> <span class="token number">0</span><span class="token punctuation">.</span>00s
  40. </code></pre></div><h3 id="_1-2-测试失败"><a href="#_1-2-测试失败" class="header-anchor">#</a> 1.2 测试失败</h3> <ul><li>测试函数 panic 就表示测试失败</li> <li>每个测试都运行在一个新线程中</li> <li>当主线程看到测试线程挂掉后,那个测试就被标记为失败</li></ul> <p>我们单独编写一个带有 panic 的测试函数,测试结果如下:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token attribute attr-name">#[cfg(test)]</span>
  41. <span class="token keyword">mod</span> <span class="token module-declaration namespace">tests</span> <span class="token punctuation">{</span>
  42. <span class="token attribute attr-name">#[test]</span>
  43. <span class="token keyword">fn</span> <span class="token function-definition function">it_works</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  44. <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">;</span>
  45. <span class="token macro property">assert_eq!</span><span class="token punctuation">(</span>result<span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  46. <span class="token punctuation">}</span>
  47. <span class="token attribute attr-name">#[test]</span>
  48. <span class="token keyword">fn</span> <span class="token function-definition function">another</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  49. <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;Test Failed!&quot;</span><span class="token punctuation">)</span>
  50. <span class="token punctuation">}</span>
  51. <span class="token punctuation">}</span>
  52. </code></pre></div><div class="language-rust extra-class"><pre class="language-rust"><code>➜ ~<span class="token operator">/</span>code<span class="token operator">/</span>rust<span class="token operator">/</span>test_demo git<span class="token punctuation">:</span><span class="token punctuation">(</span>master<span class="token punctuation">)</span> ✗ cargo test
  53. <span class="token class-name">Compiling</span> test_demo v0<span class="token number">.1</span><span class="token punctuation">.</span><span class="token number">0</span> <span class="token punctuation">(</span><span class="token operator">/</span>home<span class="token operator">/</span>cherry<span class="token operator">/</span>code<span class="token operator">/</span>rust<span class="token operator">/</span>test_demo<span class="token punctuation">)</span>
  54. <span class="token class-name">Finished</span> test <span class="token punctuation">[</span>unoptimized <span class="token operator">+</span> debuginfo<span class="token punctuation">]</span> <span class="token function">target</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token number">0</span><span class="token punctuation">.</span>28s
  55. <span class="token class-name">Running</span> <span class="token function">unittests</span> <span class="token punctuation">(</span>target<span class="token operator">/</span>debug<span class="token operator">/</span>deps<span class="token operator">/</span>test_demo<span class="token operator">-</span>357c557c333f0e0d<span class="token punctuation">)</span>
  56. running <span class="token number">2</span> tests
  57. test <span class="token namespace">tests<span class="token punctuation">::</span></span>another <span class="token punctuation">...</span> <span class="token constant">FAILED</span>
  58. test <span class="token namespace">tests<span class="token punctuation">::</span></span>it_works <span class="token punctuation">...</span> ok
  59. failures<span class="token punctuation">:</span>
  60. <span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span> <span class="token namespace">tests<span class="token punctuation">::</span></span>another stdout <span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
  61. thread <span class="token lifetime-annotation symbol">'tests</span><span class="token punctuation">::</span>another' panicked at <span class="token lifetime-annotation symbol">'Test</span> <span class="token macro property">Failed!</span>'<span class="token punctuation">,</span> src<span class="token operator">/</span>lib<span class="token punctuation">.</span>rs<span class="token punctuation">:</span><span class="token number">11</span><span class="token punctuation">:</span><span class="token number">9</span>
  62. note<span class="token punctuation">:</span> run with `<span class="token constant">RUST_BACKTRACE</span><span class="token operator">=</span><span class="token number">1</span>` environment variable to display a backtrace
  63. failures<span class="token punctuation">:</span>
  64. <span class="token namespace">tests<span class="token punctuation">::</span></span>another
  65. test result<span class="token punctuation">:</span> <span class="token constant">FAILED</span><span class="token punctuation">.</span> <span class="token number">1</span> passed<span class="token punctuation">;</span> <span class="token number">1</span> failed<span class="token punctuation">;</span> <span class="token number">0</span> ignored<span class="token punctuation">;</span> <span class="token number">0</span> measured<span class="token punctuation">;</span> <span class="token number">0</span> filtered out<span class="token punctuation">;</span> finished <span class="token keyword">in</span> <span class="token number">0</span><span class="token punctuation">.</span>00s
  66. error<span class="token punctuation">:</span> test failed<span class="token punctuation">,</span> to rerun pass '<span class="token operator">-</span><span class="token operator">-</span>lib'
  67. </code></pre></div><h2 id="二、断言"><a href="#二、断言" class="header-anchor">#</a> 二、断言</h2> <h3 id="_2-1-使用-assert-宏检查测试结果"><a href="#_2-1-使用-assert-宏检查测试结果" class="header-anchor">#</a> 2.1 使用 assert! 宏检查测试结果</h3> <ul><li>assert! 来自于标准库,用来确定某个状态是否为 true
  68. <ul><li>如果为 true,表示测试通过</li> <li>如果为 false,则调用 panic! 宏,测试失败</li></ul></li></ul> <p>我们用之前写的一个小例子,判断矩形 r1 是否能容纳矩形 r2(为了简单起见,只判断正着放,而不考虑斜着放进去)。</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">struct</span> <span class="token type-definition class-name">Rect</span> <span class="token punctuation">{</span>
  69. x<span class="token punctuation">:</span> <span class="token keyword">u32</span><span class="token punctuation">,</span>
  70. y<span class="token punctuation">:</span> <span class="token keyword">u32</span><span class="token punctuation">,</span>
  71. <span class="token punctuation">}</span>
  72. <span class="token keyword">impl</span> <span class="token class-name">Rect</span> <span class="token punctuation">{</span>
  73. <span class="token keyword">fn</span> <span class="token function-definition function">can_hold</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">,</span> other<span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token class-name">Rect</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">bool</span> <span class="token punctuation">{</span>
  74. <span class="token keyword">let</span> x <span class="token operator">=</span> <span class="token keyword">if</span> other<span class="token punctuation">.</span>x <span class="token operator">&gt;</span> other<span class="token punctuation">.</span>y <span class="token punctuation">{</span> other<span class="token punctuation">.</span>x <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> other<span class="token punctuation">.</span>y <span class="token punctuation">}</span><span class="token punctuation">;</span>
  75. <span class="token keyword">let</span> y <span class="token operator">=</span> <span class="token keyword">if</span> other<span class="token punctuation">.</span>x <span class="token operator">&lt;</span> other<span class="token punctuation">.</span>y <span class="token punctuation">{</span> other<span class="token punctuation">.</span>x <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> other<span class="token punctuation">.</span>y <span class="token punctuation">}</span><span class="token punctuation">;</span>
  76. <span class="token keyword">self</span><span class="token punctuation">.</span>x <span class="token operator">&gt;</span> x <span class="token operator">&amp;&amp;</span> <span class="token keyword">self</span><span class="token punctuation">.</span>y <span class="token operator">&gt;</span> y
  77. <span class="token punctuation">}</span>
  78. <span class="token punctuation">}</span>
  79. </code></pre></div><p>测试函数:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token attribute attr-name">#[test]</span>
  80. <span class="token keyword">fn</span> <span class="token function-definition function">larger_can_hold_smaller</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  81. <span class="token keyword">let</span> r1 <span class="token operator">=</span> <span class="token class-name">Rect</span> <span class="token punctuation">{</span> x<span class="token punctuation">:</span> <span class="token number">12</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">8</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
  82. <span class="token keyword">let</span> r2 <span class="token operator">=</span> <span class="token class-name">Rect</span> <span class="token punctuation">{</span> x<span class="token punctuation">:</span> <span class="token number">5</span><span class="token punctuation">,</span> y<span class="token punctuation">:</span> <span class="token number">10</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
  83. <span class="token macro property">assert!</span><span class="token punctuation">(</span>r1<span class="token punctuation">.</span><span class="token function">can_hold</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>r2<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  84. <span class="token punctuation">}</span>
  85. </code></pre></div><p>显然返回是 true,测试通过。</p> <h3 id="_2-2-使用-assert-eq-和-assert-ne-测试相等性"><a href="#_2-2-使用-assert-eq-和-assert-ne-测试相等性" class="header-anchor">#</a> 2.2 使用 assert_eq! 和 assert_ne! 测试相等性</h3> <ul><li>都来自标准库</li> <li>判断两个参数是否相等或不等</li> <li>实际上,它们使用的就是 <code>==</code> 和 <code>!=</code> 运算符</li> <li>如果断言失败,该宏会自动打印出两个参数的值
  86. <ul><li>使用 debug 格式打印参数</li> <li>要求参数实现 PartialEq 和 Debug Traits(所有基本类型和标准库里大部分类型基本都实现了)</li> <li>如果使用 <code>assert!</code> 宏,则只会告知测试结果而不会打印出两个参数的值</li></ul></li></ul> <p>我们再写一个简单的例子,将一个数加 2,判断两个值是否相等。</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">fn</span> <span class="token function-definition function">add_two</span><span class="token punctuation">(</span>a<span class="token punctuation">:</span> <span class="token keyword">i32</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">i32</span> <span class="token punctuation">{</span>
  87. a <span class="token operator">+</span> <span class="token number">2</span>
  88. <span class="token punctuation">}</span>
  89. <span class="token attribute attr-name">#[test]</span>
  90. <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">it_add_two</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  91. <span class="token macro property">assert_eq!</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token function">add_two</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  92. <span class="token punctuation">}</span>
  93. </code></pre></div><p>结果显然是正确的,若我们更改函数逻辑,把加 2 改成加 3,则运行测试结果为:</p> <div class="language-rust extra-class"><pre class="language-rust"><code>➜ ~<span class="token operator">/</span>code<span class="token operator">/</span>rust<span class="token operator">/</span>test_demo git<span class="token punctuation">:</span><span class="token punctuation">(</span>master<span class="token punctuation">)</span> ✗ cargo test
  94. <span class="token class-name">Compiling</span> test_demo v0<span class="token number">.1</span><span class="token punctuation">.</span><span class="token number">0</span> <span class="token punctuation">(</span><span class="token operator">/</span>home<span class="token operator">/</span>cherry<span class="token operator">/</span>code<span class="token operator">/</span>rust<span class="token operator">/</span>test_demo<span class="token punctuation">)</span>
  95. <span class="token class-name">Finished</span> test <span class="token punctuation">[</span>unoptimized <span class="token operator">+</span> debuginfo<span class="token punctuation">]</span> <span class="token function">target</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token number">0</span><span class="token punctuation">.</span>39s
  96. <span class="token class-name">Running</span> <span class="token function">unittests</span> <span class="token punctuation">(</span>target<span class="token operator">/</span>debug<span class="token operator">/</span>deps<span class="token operator">/</span>test_demo<span class="token operator">-</span>357c557c333f0e0d<span class="token punctuation">)</span>
  97. running <span class="token number">3</span> tests
  98. test <span class="token namespace">tests<span class="token punctuation">::</span></span>it_add_two <span class="token punctuation">...</span> <span class="token constant">FAILED</span>
  99. test <span class="token namespace">tests<span class="token punctuation">::</span></span>it_works <span class="token punctuation">...</span> ok
  100. test <span class="token namespace">tests<span class="token punctuation">::</span></span>larger_can_hold_smaller <span class="token punctuation">...</span> ok
  101. failures<span class="token punctuation">:</span>
  102. <span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span> <span class="token namespace">tests<span class="token punctuation">::</span></span>it_add_two stdout <span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
  103. thread <span class="token lifetime-annotation symbol">'tests</span><span class="token punctuation">::</span>it_add_two' panicked at <span class="token lifetime-annotation symbol">'assertion</span> failed<span class="token punctuation">:</span> `<span class="token punctuation">(</span>left <span class="token operator">==</span> right<span class="token punctuation">)</span>`
  104. left<span class="token punctuation">:</span> `<span class="token number">4</span>`<span class="token punctuation">,</span>
  105. right<span class="token punctuation">:</span> `<span class="token number">5</span>`'<span class="token punctuation">,</span> src<span class="token operator">/</span>lib<span class="token punctuation">.</span>rs<span class="token punctuation">:</span><span class="token number">37</span><span class="token punctuation">:</span><span class="token number">9</span>
  106. note<span class="token punctuation">:</span> run with `<span class="token constant">RUST_BACKTRACE</span><span class="token operator">=</span><span class="token number">1</span>` environment variable to display a backtrace
  107. failures<span class="token punctuation">:</span>
  108. <span class="token namespace">tests<span class="token punctuation">::</span></span>it_add_two
  109. test result<span class="token punctuation">:</span> <span class="token constant">FAILED</span><span class="token punctuation">.</span> <span class="token number">2</span> passed<span class="token punctuation">;</span> <span class="token number">1</span> failed<span class="token punctuation">;</span> <span class="token number">0</span> ignored<span class="token punctuation">;</span> <span class="token number">0</span> measured<span class="token punctuation">;</span> <span class="token number">0</span> filtered out<span class="token punctuation">;</span> finished <span class="token keyword">in</span> <span class="token number">0</span><span class="token punctuation">.</span>00s
  110. error<span class="token punctuation">:</span> test failed<span class="token punctuation">,</span> to rerun pass '<span class="token operator">-</span><span class="token operator">-</span>lib'
  111. </code></pre></div><p>编译器将会自动给出两个参数的值(左值和右值),若将宏改成 <code>assert_ne!</code> 测试结果又将变成正确。</p> <h2 id="三、自定义错误消息"><a href="#三、自定义错误消息" class="header-anchor">#</a> 三、自定义错误消息</h2> <p>TODO</p></div> <footer class="page-edit"><!----> <!----></footer> <!----> </main></div><div class="global-ui"></div></div>
  112. <script src="/rust_camp_tutorial/assets/js/app.d7ab8f65.js" defer></script><script src="/rust_camp_tutorial/assets/js/2.3dc1b8de.js" defer></script><script src="/rust_camp_tutorial/assets/js/1.7f771cfb.js" defer></script><script src="/rust_camp_tutorial/assets/js/30.72f41aed.js" defer></script>
  113. </body>
  114. </html>