{"id":7714,"date":"2019-11-21T21:18:28","date_gmt":"2019-11-21T21:18:28","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7714"},"modified":"2019-11-21T21:18:28","modified_gmt":"2019-11-21T21:18:28","slug":"basics-of-unit-testing-in-rust","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/basics-of-unit-testing-in-rust\/","title":{"rendered":"Basics of unit testing in Rust"},"content":{"rendered":"<p>I&#8217;m messing around with some Rust code at the moment, so expect a few posts in the near future. In this post I&#8217;ve going to jump straight into unit testing in Rust.<\/p>\n<p>You don&#8217;t need to have a dependency on any unit testing frameworks as Rust has a unit testing framework integrated within it. <\/p>\n<p>Our unit tests can sit along side our existing code using the conditional compilation annotation <em>#[cfg(test)]<\/em>. Let&#8217;s create a simple example test, which assumed we have a Stack implementation to test<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#&#x5B;cfg(test)]\r\nmod tests {\r\n    use super::*;\r\n\r\n    #&#x5B;test]\r\n    fn initial_state() {\r\n        let s: Stack&lt;i32&gt; = Stack::new();\r\n        assert_eq!(s.length, 0);\r\n    }\r\n}\r\n<\/pre>\n<p>The following line simply allows us to use code from the parent scope (i.e. allows us to use the Stack code). <\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nuse super::*;\r\n<\/pre>\n<p>Next up with have the <em>#[test]<\/em> annotation which (probably fairly obviously) declares the function <em>initial_state<\/em> to be a test function. <\/p>\n<p>Ofcourse we need some form of assertation code, hence the <em>assert_eq!<\/em> macro.<\/p>\n<p>We can also test whether our code panics by placed the <em>#[should_panic]<\/em> annotation after the <em>#[test]<\/em> annotation. This denotes that the system under test should panic (similar to exceptions in other languages).<\/p>\n<p>Some times we need to ignore a test, in such cases we can use the <em>#[ignore]<\/em> annotation<\/p>\n<p>Obviously we need run our tests, we can use cargo for this, simple run <\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ncargo test\r\n<\/pre>\n<p>We can also run tests in parallel using the <em>-test-threads<\/em> option, for example if we want test to be run in parallel on two threads, we use<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ncargo test -- --test-threads=2\r\n<\/pre>\n<p>See further options around controlling how your tests are run <a href=\"https:\/\/doc.rust-lang.org\/book\/ch11-02-running-tests.html\" rel=\"noopener noreferrer\" target=\"_blank\">controlling how your tests are run<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m messing around with some Rust code at the moment, so expect a few posts in the near future. In this post I&#8217;ve going to jump straight into unit testing in Rust. You don&#8217;t need to have a dependency on any unit testing frameworks as Rust has a unit testing framework integrated within it. Our [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[191],"tags":[],"class_list":["post-7714","post","type-post","status-publish","format-standard","hentry","category-rust"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7714"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7714\/revisions"}],"predecessor-version":[{"id":7721,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7714\/revisions\/7721"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}