qmk_firmware/assets/unit_testing.md.CGe2-PJx.js

16 lines
11 KiB
JavaScript
Raw Normal View History

import { _ as _export_sfc, c as createElementBlock, o as openBlock, a8 as createStaticVNode } from "./chunks/framework.Clpp4x2N.js";
const __pageData = JSON.parse('{"title":"Unit Testing","description":"","frontmatter":{},"headers":[],"relativePath":"unit_testing.md","filePath":"unit_testing.md"}');
const _sfc_main = { name: "unit_testing.md" };
const _hoisted_1 = /* @__PURE__ */ createStaticVNode('<h1 id="unit-testing" tabindex="-1">Unit Testing <a class="header-anchor" href="#unit-testing" aria-label="Permalink to &quot;Unit Testing&quot;"></a></h1><p>If you are new to unit testing, then you can find many good resources on internet. However most of it is scattered around in small pieces here and there, and there&#39;s also many different opinions, so I won&#39;t give any recommendations.</p><p>Instead I recommend these two books, explaining two different styles of Unit Testing in detail.</p><ul><li>&quot;Test Driven Development: By Example: Kent Beck&quot;</li><li>&quot;Growing Object-Oriented Software, Guided By Tests: Steve Freeman, Nat Pryce&quot;</li></ul><p>If you prefer videos there are Uncle Bob&#39;s <a href="https://cleancoders.com/" target="_blank" rel="noreferrer">Clean Coders Videos</a>, which unfortunately cost quite a bit, especially if you want to watch many of them. But James Shore has a free <a href="https://www.jamesshore.com/Blog/Lets-Play" target="_blank" rel="noreferrer">Let&#39;s Play</a> video series.</p><h2 id="google-test-and-google-mock" tabindex="-1">Google Test and Google Mock <a class="header-anchor" href="#google-test-and-google-mock" aria-label="Permalink to &quot;Google Test and Google Mock&quot;"></a></h2><p>It&#39;s possible to Unit Test your code using <a href="https://github.com/google/googletest" target="_blank" rel="noreferrer">Google Test</a>. The Google Test framework also includes another component for writing testing mocks and stubs, called &quot;Google Mock&quot;. For information how to write the actual tests, please refer to the documentation on that site.</p><h2 id="use-of-c" tabindex="-1">Use of C++ <a class="header-anchor" href="#use-of-c" aria-label="Permalink to &quot;Use of C++&quot;"></a></h2><p>Note that Google Test and therefore any test has to be written in C++, even if the rest of the QMK codebases is written in C. This should hopefully not be a problem even if you don&#39;t know any C++, since there&#39;s quite clear documentation and examples of the required C++ features, and you can write the rest of the test code almost as you would write normal C. Note that some compiler errors which you might get can look quite scary, but just read carefully what it says, and you should be ok.</p><p>One thing to remember, is that you have to append <code>extern &quot;C&quot;</code> around all of your C file includes.</p><h2 id="adding-tests-for-new-or-existing-features" tabindex="-1">Adding Tests for New or Existing Features <a class="header-anchor" href="#adding-tests-for-new-or-existing-features" aria-label="Permalink to &quot;Adding Tests for New or Existing Features&quot;"></a></h2><p>If you want to unit test a feature, take a look at some of the existing tests, for example those in the <code>quantum/sequencer/tests</code> folder. Then follow the steps below to create a similar structure.</p><ol><li>If it doesn&#39;t already exist, add a test subfolder to the folder containing the feature.</li><li>Create a <code>testlist.mk</code> and a <code>rules.mk</code> file in that folder.</li><li>Include those files from the root folder <code>testlist.mk</code>and <code>build_test.mk</code> respectively.</li><li>Add a new name for your testgroup to the <code>testlist.mk</code> file. Each group defined there will be a separate executable. And that&#39;s how you can support mocking out different parts. Note that it&#39;s worth adding some common prefix, just like it&#39;s done for the existing tests. The reason for that is that the make command allows substring filtering, so this way you can easily run a subset of the tests.</li><li>Define the source files and required options in the <code>rules.mk</code> file. <ul><li><code>_SRC</code> for source files</li><li><code>_DEFS</code> for additional defines</li><li><code>_INC</code> for additional include folders</li></ul></li><li>Write the tests in a new cpp file inside the test folder you created. That file has to be one of the files included from the <code>rules.m
const _hoisted_34 = [
_hoisted_1
];
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return openBlock(), createElementBlock("div", null, _hoisted_34);
}
const unit_testing = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
export {
__pageData,
unit_testing as default
};