[{"data":1,"prerenderedAt":22},["ShallowReactive",2],{"page:\u002Fresource-library\u002Fframeworks-for-embedded-tdd":3},{"path":4,"layout":5,"title":6,"description":7,"ogImage":8,"post":9,"html":21},"\u002Fresource-library\u002Fframeworks-for-embedded-tdd","post","Frameworks for embedded TDD","Unity, Check, GoogleTest, and more compared for unit testing embedded C: test runners, mocking, and OS needs, and why the author uses Unity day to day.","\u002Fimages\u002Fresource-library\u002Fembedded-tdd-challenges\u002Fc-source-bcm2835-gpio-init-social.jpg",{"author":10,"published":11,"updated":12,"tags":13,"image":20,"imageAlt":12},"Austin Glaser","2017-04-11",null,[14,17],{"name":15,"slug":16},"Testing and quality","testing",{"name":18,"slug":19},"Embedded systems and software","embedded-software","\u002Fimages\u002Fresource-library\u002Fembedded-tdd-challenges\u002Fc-source-bcm2835-gpio-init.webp","\u003Cp>Perhaps the most critical element for unit testing is a test framework. In this blog post, I'll\n  discuss various options for the C language, and provide a brief analysis of pros and cons.\u003C\u002Fp>\n\u003Cp>I've experimented with all of the below frameworks, but the one I use on a day-to-day basis is\n  Unity.\u003C\u002Fp>\n\u003Ch2>Considerations\u003C\u002Fh2>\n\u003Cp>Depending on your situation, there are some important factors to consider when choosing a test\n  framework. Below I've listed a couple which I've had occasion to think a lot about.\u003C\u002Fp>\n\u003Ch3>Test runners\u003C\u002Fh3>\n\u003Cp>Most embedded programming uses C, and all the frameworks I discuss below are for C or C++. This\n  in and of itself can present some difficulties. Many unit test frameworks take advantage of\n  language features known as \"introspection.\" Effectively, this is the ability to determine\n  information about the structure of the executing program at runtime. It can be useful for tests\n  that self-register with the framework, and is used extensively for frameworks targeted to\n  languages like Ruby, Python, and Java.\u003C\u002Fp>\n\u003Cp>Practically, this means that test frameworks in C require more setup work on the part of the\n  programmer. Most frameworks will need a manually written \"runner\" which invokes the test suite,\n  leading to possible errors such as forgetting to add a particular test to the runner.\u003C\u002Fp>\n\u003Cp>There are tools to help automate this process, but they fall outside the purview of a strict\n  \"test framework\" -- they're more akin to full build systems in their own right. I'll address these\n  options in a future blog post.\u003C\u002Fp>\n\u003Cp>C++ has some features allowing introspection, so test suites for that language \u003Cem>can\u003C\u002Fem>\n  implement automatic test registration. In most cases, it's possible to compile and test a project\n  written entirely in C as C++. This can often be a good solution, but can be jarring for\n  programmers unused to the language.\u003C\u002Fp>\n\u003Ch3>Running on the hardware\u003C\u002Fh3>\n\u003Cp>The ThrowTheSwitch team has a \u003Ca href=\"http:\u002F\u002Fwww.throwtheswitch.org\u002Fbuild\u002Fwhich\" target=\"_blank\"\n  rel=\"noopener\">great writeup\u003C\u002Fa> discussing the best location to run tests. I won't repeat their\n  reasoning here, but I will note that it lays out three different options: the development system\n  through a simulator, the workstation natively, or the system hardware. Generally, it makes sense\n  to use one of the first two options, but there are situations (or different tests) where it might\n  make sense to do otherwise.\u003C\u002Fp>\n\u003Cp>With that said, the choice of your test framework will factor into whether it's \u003Cem>possible\u003C\u002Fem>\n  to do any of these. Depending on the type of simulator available for your platform, it may be\n  difficult to use a tool which depends on being run under an operating system (such as check). This\n  will certainly \u003Cem>not\u003C\u002Fem> work on the bare-metal hardware itself. On the other hand, having\n  features like the ability to run tests in separate address spaces can be really nice when a\n  failing test might cause a segmentation fault.\u003C\u002Fp>\n\u003Ch2>Framework options\u003C\u002Fh2>\n\u003Cp>There are many more test frameworks available than the ones I discuss here. My main criterion for\n  inclusion in this list was a project still under active development. With that said, some of the\n  ones not mentioned (CUnit, embUnit, AceUnit) may fulfill your requirements as-is, without the need\n  for a developer team to respond to bug reports, feature requests, etc.\u003C\u002Fp>\n\u003Ch3>Unity\u003C\u002Fh3>\n\u003Cp>As I said before, I use this test framework every day. It's quite lightweight, and highly\n  portable. The developers claim that it can be ported to just about any architecture, from a\n  high-powered x86 machine to an 8-bit microcontroller, including ones with esoteric features such\n  as weird integer widths. It's specifically targeted to embedded development.\u003C\u002Fp>\n\u003Cp>The price it pays for its portability is a dearth of high-level features like address space\n  isolation, or automatic runner generation and test integration.\u003C\u002Fp>\n\u003Cp>It integrates well with the ThrowTheSwitch suite of tools, which includes \u003Ca\n  href=\"http:\u002F\u002Fwww.throwtheswitch.org\u002Fcmock\u002F\" target=\"_blank\" rel=\"noopener\">CMock\u003C\u002Fa>, \u003Ca\n  href=\"http:\u002F\u002Fwww.throwtheswitch.org\u002Fcexception\u002F\" target=\"_blank\" rel=\"noopener\">CException\u003C\u002Fa>,\n  and \u003Ca href=\"http:\u002F\u002Fwww.throwtheswitch.org\u002Fceedling\" target=\"_blank\" rel=\"noopener\">Ceedling\u003C\u002Fa>.\n  When used together, many of the disadvantages of Unity's simplicity are mitigated.\u003C\u002Fp>\n\u003Cp>Language: C\u003C\u002Fp>\n\u003Cp>\u003Cstrong>Pros:\u003C\u002Fstrong>\u003C\u002Fp>\n\u003Cul>\n\u003Cli>Lightweight\u003C\u002Fli>\n\u003Cli>Highly portable\u003C\u002Fli>\n\u003Cli>Actively developed\u003C\u002Fli>\n\u003Cli>Integrates with mock framework\u003C\u002Fli>\n\u003Cli>Highly configurable (e.g., to support custom data types)\u003C\u002Fli>\u003C\u002Ful>\n\u003Cp>\u003Cstrong>Cons:\u003C\u002Fstrong>\u003C\u002Fp>\n\u003Cul>\n\u003Cli>Relatively simplistic\u003C\u002Fli>\n\u003Cli>No address-space isolation\u003C\u002Fli>\n\u003Cli>No automatic test registration (but see above about Ceedling integration)\u003C\u002Fli>\u003C\u002Ful>\n\u003Ch3>Check\u003C\u002Fh3>\n\u003Cp>\u003Ca href=\"https:\u002F\u002Flibcheck.github.io\u002Fcheck\u002F\" target=\"_blank\" rel=\"noopener\">Check\u003C\u002Fa> is another\n  relatively lightweight C unit testing framework. Unlike Unity, it's not designed with bare-metal\n  embedded development in mind. Instead, it focuses on being safe and simple to use. Its main claim\n  to fame is that it runs each test in an isolated address space, which means that invalid memory\n  accesses can be caught. In contrast, a test using Unity will die outright on a segmentation\n  fault.\u003C\u002Fp>\n\u003Cp>It has a few more convenience functions than Unity as well, including parameterized tests which\n  can be convenient for writing routines that should validate output over a range of different\n  values.\u003C\u002Fp>\n\u003Cp>check pays some prices for running its tests in separate processes. The first is, clearly, a\n  dependence on the operating system. This means that it would be very difficult to directly port to\n  an embedded system, and may have trouble when cross-compiled to run under a simulator.\u003C\u002Fp>\n\u003Cp>Secondly, spawning a child process is far more expensive than a simple function call, so check\n  tends to bog down when it has to run many tests.\u003C\u002Fp>\n\u003Cp>Like any other pure-C framework, test runners have to be written manually.\u003C\u002Fp>\n\u003Cp>Language: C\u003C\u002Fp>\n\u003Cp>\u003Cstrong>Pros:\u003C\u002Fstrong>\u003C\u002Fp>\n\u003Cul>\n\u003Cli>Runs tests in an isolated address space\u003C\u002Fli>\u003C\u002Ful>\n\u003Cp>\u003Cstrong>Cons:\u003C\u002Fstrong>\u003C\u002Fp>\n\u003Cul>\n\u003Cli>No automatic test registration\u003C\u002Fli>\n\u003Cli>Relies on OS support\u003C\u002Fli>\n\u003Cli>Can be slow\u003C\u002Fli>\n\u003Cli>Less configurable for esoteric platforms\u003C\u002Fli>\n\u003Cli>No direct mock framework integration\u003C\u002Fli>\u003C\u002Ful>\n\u003Ch3>GoogleTest\u003C\u002Fh3>\n\u003Cp>\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fgoogle\u002Fgoogletest\" target=\"_blank\" rel=\"noopener\">GoogleTest\u003C\u002Fa> is\n  Google's unit test framework. It's written in C++, but can be used to test C applications with a\n  little tweaking. It supports the use of Google Mock, which is necessary for testing against\n  hardware-dependent modules.\u003C\u002Fp>\n\u003Cp>GoogleTest does support running tests in separate address spaces, but doesn't do this by default.\n  These tests are called \"Death Tests,\" and they're mostly used for catching \u003Cem>expected\u003C\u002Fem>\n  failures, rather than a protection against unexpected segfaults and the like.\u003C\u002Fp>\n\u003Cp>This test framework has far more features than I have the space for here, but its \u003Ca\n  href=\"https:\u002F\u002Fgithub.com\u002Fgoogle\u002Fgoogletest\u002Fblob\u002Fmaster\u002Fgoogletest\u002Fdocs\u002FPrimer.md\" target=\"_blank\"\n  rel=\"noopener\">documentation\u003C\u002Fa> is very good, and worth glancing over.\u003C\u002Fp>\n\u003Cp>Language: C++\u003C\u002Fp>\n\u003Cp>\u003Cstrong>Pros:\u003C\u002Fstrong>\u003C\u002Fp>\n\u003Cul>\n\u003Cli>Automatic test registration\u003C\u002Fli>\n\u003Cli>Works similarly to most familiar xUnit (e.g., JUnit) test frameworks\u003C\u002Fli>\n\u003Cli>Integrates with mock framework\u003C\u002Fli>\n\u003Cli>Possibility of isolated tests\u003C\u002Fli>\n\u003Cli>\u003Cem>Very\u003C\u002Fem> full-featured\u003C\u002Fli>\u003C\u002Ful>\n\u003Cp>\u003Cstrong>Cons:\u003C\u002Fstrong>\u003C\u002Fp>\n\u003Cul>\n\u003Cli>Relies on OS support\u003C\u002Fli>\n\u003Cli>Less configurable for esoteric platforms\u003C\u002Fli>\u003C\u002Ful>",1795194793325]