如何单元测试代码生成器? [英] How should I unit test a code-generator?

查看:129
本文介绍了如何单元测试代码生成器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我知道的一个困难和开放式的问题,但我想我会把它扔到地板上,看看有没有任何有趣的建议。



我开发了一个代码生成器,它将我们的python接口转换为我们的C ++代码(通过SWIG生成),并生成将它公开为WebServices所需的代码。当我开发这个代码,我使用TDD,但我发现我的测试是脆弱的地狱。因为每个测试本质上都想验证对于给定位的输入代码(恰好是一个C ++头),我会得到一个给定的输出代码位我写了一个小引擎从XML输入文件读取测试定义并生成测试这些预期的情况。



问题是我恐怕要修改代码。这和单元测试本身的事实是:复杂的,b:brittle。



所以我试图想出这个问题的替代方法,打我,我可能是解决它的错误的方式。也许我需要更多地关注结果,IE:我生成的代码实际运行和做我想要的,而不是,代码看起来我想要的方式。



有人有任何类似的经验,他们会关心分享吗?

解决方案

总结我对自己的代码生成器的经验,然后回去并重新阅读你的问题,发现你已经自己触及了相同的问题,专注于执行结果,而不是代码布局/外观。



问题是,这很难测试,生成的代码可能不适合在单元测试系统的环境中实际运行,以及如何对预期结果进行编码? p>

我发现你需要将代码生成器分解成更小的部分并对其进行单元测试。单元测试一个完整的代码生成器更像是集成测试比单元测试,如果你问我。


This is a difficult and open-ended question I know, but I thought I'd throw it to the floor and see if anyone had any interesting suggestions.

I have developed a code-generator that takes our python interface to our C++ code (generated via SWIG) and generates code needed to expose this as WebServices. When I developed this code I did it using TDD, but I've found my tests to be brittle as hell. Because each test essentially wanted to verify that for a given bit of input code (which happens to be a C++ header) I'd get a given bit of outputted code I wrote a small engine that reads test definitions from XML input files and generates test cases from these expectations.

The problem is I dread going in to modify the code at all. That and the fact that the unit tests themselves are a: complex, and b: brittle.

So I'm trying to think of alternative approaches to this problem, and it strikes me I'm perhaps tackling it the wrong way. Maybe I need to focus more on the outcome, IE: does the code I generate actually run and do what I want it to, rather than, does the code look the way I want it to.

Has anyone got any experiences of something similar to this they would care to share?

解决方案

I started writing up a summary of my experience with my own code generator, then went back and re-read your question and found you had already touched upon the same issues yourself, focus on the execution results instead of the code layout/look.

Problem is, this is hard to test, the generated code might not be suited to actually run in the environment of the unit test system, and how do you encode the expected results?

I've found that you need to break down the code generator into smaller pieces and unit test those. Unit testing a full code generator is more like integration testing than unit testing if you ask me.

这篇关于如何单元测试代码生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆