C嵌入式自动单元测试生成

发布于 2024-09-01 11:08:44 字数 242 浏览 5 评论 0原文

是否有任何软件可以在 C 和嵌入式应用程序中生成单元测试?我之所以问这个问题,是因为我的老板告诉我,他听某人说“你需要一个工具来分析代码并自动创建 80% 的相关测试用例,剩下的 20% 需要你用所有的时间并专注于”,否则这将花费“太多时间”。

我对这种说法非常怀疑,并且无法清楚地看到可以自动生成什么样的测试以及它们是否有任何好处。

不过,我可以看到可以为 API 自动生成接口单元测试。

那么有人可以告诉我这个问题吗?

Is there any SW to generate unit tests in C and embedded applications? The reason I am asking is that my boss told me he heard from someone that "You need a tool to analyze the code and create 80% of all relevant testcases automatically, the remaining 20% you use all your time and focus on", else it would take "too much time".

I am very skeptic about this statement and can't see clearly what kind of tests that could be auto generated and if they would be any good at all.

I can, however, see that it would be possible to generate interface unit tests automatically for the API:s.

So can someone enlighten me on this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

泅渡 2024-09-08 11:08:44

我推荐 API Sanity Checker 工具:

共享 C/C++ 库的基本单元测试的自动生成器。它能够为参数生成合理的(在大多数情况下,但不幸的是不是全部情况)输入数据,并通过分析标头中的声明为 API 中的每个函数编写简单的(“健全”或“浅薄”质量)测试用例文件。

生成的测试的质量允许检查简单用例中是否存在严重错误。该工具能够构建和执行生成的测试,并检测崩溃(段错误)、中止、各种发出的信号、非零程序返回代码和程序挂起。

独特功能:

  • 自动生成输入参数和测试数据(即使是复杂的数据类型)
  • 现代 专门类型而不是固定装置和模板

请参阅 自由类型2

输入图片此处描述

我是该项目的作者,您可以向我询问有关该项目的任何问题。

I recommend API Sanity Checker tool:

An automatic generator of basic unit tests for a shared C/C++ library. It is able to generate reasonable (in most, but unfortunately not all, cases) input data for parameters and compose simple ("sanity" or "shallow"-quality) test cases for every function in the API through the analysis of declarations in header files.

The quality of generated tests allows to check absence of critical errors in simple use cases. The tool is able to build and execute generated tests and detect crashes (segfaults), aborts, all kinds of emitted signals, non-zero program return code and program hanging.

Unique features:

  • Automatic generation of input arguments and test data (even for complex data types)
  • Modern specialized types instead of fixtures and templates

See examples for FreeType2.

enter image description here

I'm author of this project and you can ask me any questions on it.

司马昭之心 2024-09-08 11:08:44

你的老板搞错了方向。

据我所知,没有任何工具可以为您生成单元测试。

他可能错误的是代码覆盖率单元测试。虽然相关,但它们实际上是不同的问题。

代码覆盖率将检测您的代码,一旦运行完成,您就可以了解运行中使用了多少源代码。这在单元测试时非常有用,因为它将有效地向您显示已测试的位置以及需要将工作重点放在何处。

覆盖前三分之二的代码相当容易,但收益递减意味着要接近神奇的 100% 需要花费大量的时间和精力。

Your boss has got the wrong end of the stick.

I know of no tools that will generate unit tests for you.

What he may be mistaking is code coverage and unit testing. While related they are in fact separate issues.

Code coverage will instrument your code and once finished running give you the low down on how much of your source was used in the run. This is useful when unit testing as it will effectively show you where you have tested and where you need to focus your work.

It is fairly easy to get the first two thirds of code covered but diminishing returns means to get near a magic 100% takes a lot of time and effort.

咋地 2024-09-08 11:08:44

谷歌搜索“单元测试生成器”会发现很多东西,但我不知道它们是否有任何好处,或者它们是否适合您的情况。

这不是单元测试,但您可以使用 lint 或相关工具进行一些代码检查。请参阅:http://www.lysator.liu.se/c/ten- commandments.html 我认为当前的一个开源工具是splint http://www.splint.org/< /a>

Jon Bentley 的书籍对“脚手架”代码(包括测试脚手架)的作用进行了一些很好的讨论。

Googling "unit test generator" turns up a lot of things, but I do not know if they are any good, or if they'll suit your case.

It is not unit testing, but you can do some code checking with lint or related tools. See: http://www.lysator.liu.se/c/ten-commandments.html I think a current open source tool is splint http://www.splint.org/

Jon Bentley's books have some good discussion of the role of "scaffolding" code, including test scaffolds.

尝蛊 2024-09-08 11:08:44

首先,单元测试和生成单元测试是什么意思?

您的意思是生成一个框架、一个测试工具,还是生成一个带有数据和数据检查或实际调用您的代码的断言的测试。并且,在后一种情况下,该测试是如何生成的?

更根本的是,你为什么要测试?您是否遵循需要一定级别测试的标准,或者您“只是”试图降低开发后期阶段的风险和成本?或者,您可能正在现有系统上构建,只是想确保不会破坏任何现有功能。

之前的答案提到了 Cantata,我们最近发布了一个新版本,其中包含一个名为“基线测试”的组件。这可能正是您正在寻找的。它将为 C 代码创建一组单元测试,其中包含很有可能充分运用每个源文件的测试用例。该工具通过读取您的源代码并生成一组测试来实现这一目标,这些测试驱动每条路径的执行,旨在实现您期望的覆盖率目标——100% 的声明、决策甚至 MC/DC 覆盖率。目的是作为遗留系统持续开发的一部分对源代码进行“基准化”,或者在功能测试或系统测试后填补覆盖范围的空白。

请参阅 Cantata++ 网页了解更多信息(和免费评估)

First of all, what do you mean by unit test and generate unit tests?

Do you mean generate a framework, a test harness or do you mean generate a test with data and data checks or assertions that actually calls your code. And, in the latter case, how is that test generated?

More fundamentally, why are you testing? Are you following a standard that requires a certain level of testing, or are you 'just' trying to reduce risk and cost in later stages of development? Or maybe you are building on an existing system and just want to make sure you do not break any existing functionality.

A previous answer mentioned Cantata, we have recently released a new version with a component called 'baseline testing'. This may be just what you are looking for. It will create, for C code, a set of unit tests, containing test cases that stand a good chance of fully exercising each of your source files. The tool achieves this by reading your source and producing a set of test that drive the execution down each path aiming to achieve your desired coverage target – 100% statement, decision or even MC/DC coverage. The intent is to 'baseline' your source code as part of the ongoing development of a legacy system, or to fill in coverage gaps after functional or maybe system testing.

See the Cantata++ webpage for more information (and free evaluations)

少年亿悲伤 2024-09-08 11:08:44

我们在这里使用 CANtata 来生成单元测试/代码覆盖率。它很不错,尽管我认为它有点贵。

We use CANtata here where I work for generating unit tests / code coverage. Its decent, though I imagine it is a bit pricey.

我们的影子 2024-09-08 11:08:44

我们使用 IBM RTRT

http://www-01.ibm。 com/software/awdtools/test/realtime/index.html

虽然在我们的用例中我们不使用它来生成测试,但我看到了至少生成骨架的一些可能性。

We use IBM RTRT

http://www-01.ibm.com/software/awdtools/test/realtime/index.html

Although in our use case we don't use it to generate the tests, but I saw some possibilities to generate at least skeleton.

过气美图社 2024-09-08 11:08:44

是否有任何软件可以在 C 和嵌入式应用程序中生成单元测试?

是的。 IBM的Rational Test Realtime将是不错的选择。

一个分析代码并自动创建 80% 相关测试用例的工具

不。没有任何工具可以为所有类型的 C 源代码完成这项工作。

但对于某些情况是可以的。例如,就我而言,我有大量的C源代码需要由客户测试。但是因为每个源文件都非常相似,所以我们制作了一个小工具来读取每个文件并生成测试用例(到单元测试工具的脚本语言),然后由单元测试工具执行。是的,在这种情况下,它可以节省 80% 的精力。

所以你可以考虑这样做,找出类似的源代码,并制作自己的工具来生成类似的测试用例。

Is there any SW to generate unit tests in C and embedded applications?

Yes. Rational Test Realtime of IBM will be the good choice.

a tool to analyze the code and create 80% of all relevant testcases automatically

No. There is no tool that can do this work for all type of C source codes.

But yes for some cases. For example, in my case, I have big amount of C source codes are required to be tested by customer. But because every source file are quite similar, so we make a small tool that read each file and generate test case (to the scripting language of the Unit Testing Tool) and then execute by the Unit Testing Tool. And yes, in this case, it saves 80% effort.

So you could consider to do this, find out the similar source codes, and make your own tool that can generate test case for the similar.

黒涩兲箜 2024-09-08 11:08:44
  1. 我使用了 Parasoft C++Test。它相当昂贵,但可以快速完成工作。生成 C 和 C++ 的所有单元测试和模拟函数,并使您达到 100% 的代码覆盖率。它还附带了自己的单元测试框架和模拟框架,因此如果您想编写自己的单元测试,也可以这样做。
  2. 另一个工具是 Tessy ,它也不便宜。它还为您的整个代码生成单元测试和模拟函数。 Tessy支持C和C++,但从我的简短使用经验来看,它对C代码更友好。在 C++ 的情况下,您可能需要深入研究自定义才能使测试生成正常工作。
  1. I used Parasoft C++Test. It's quite pricy but gets the job done and fast. Generates all the unit tests and mock functions for both C and C++ and gets you to 100% code coverage. It also comes with its own unit tests framework and mocking framework so that if you want to write your own unit tests you can also do it.
  2. Another tool is Tessy which isn't cheap either. It also generates unit tests and mock functions for your entire code. Tessy supports C and C++ but from my brief experience with it, it is more friendly for C code. In cases of C++ you might need to dig into customizations in order to get the generation of tests to work properly.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文