简体   繁体   English

自动生成单元测试

[英]Generating Unit Tests Automatically

I have a web tool which when queried returns generated Java classes based upon the arguments in the URL. 我有一个Web工具,在查询时返回基于URL中的参数生成的Java类。

The classes we retrieve from the webserver change daily and we need to ensure that they still can process known inputs. 我们从Web服务器检索的类每天都在变化,我们需要确保它们仍然可以处理已知的输入。

Note these classes do not test the webserver, they run locally and transform xml into a custom format. 请注意,这些类不测试Web服务器,它们在本地运行并将xml转换为自定义格式。 I am not testing the webserver. 我没有测试网络服务器。

These classes must then be placed in specific package structure compiled and run against a known set of input data and compared against known output data. 然后必须将这些类放在特定的包结构中,编译并针对一组已知的输入数据运行,并与已知的输出数据进行比较。

I would like to do this automatically each night to make sure that the generated classes are correct. 我想每晚自动执行此操作以确保生成的类是正确的。

What is the best way to achieve this? 实现这一目标的最佳方法是什么?

Specifically whats the best way to: 具体是什么最好的方式:

  1. retrieve the code from a webserver and place it in a file 从Web服务器检索代码并将其放在文件中
  2. compile the code and then call it 编译代码然后调用它

I'm sure a mix of junit and ant will be able to achieve this but is there and standard solution / approach for this? 我确信junit和ant的组合能够实现这个目标,但是有没有标准的解决方案/方法呢?

First up, to answer your question: No, I do not think that there is a standard approach for this. 首先,回答你的问题:不,我不认为有一个标准的方法。 This sounds like quite an unusual situation ;-) 这听起来很不寻常;-)

Given that, what I would do is to write your JUnit tests to all call a class GeneratedCode , and then, once you download the code, rename the class to GeneratedCode , compile, and run your unit tests. 鉴于此,我要做的是将JUnit测试写入所有调用类GeneratedCode ,然后,一旦下载代码,将类重命名为GeneratedCode ,编译并运行单元测试。

You have the same goal as continuous integration ;-) 你有与持续集成相同的目标;-)

Maybe a bit overkill for this simple task, but this is the standard way to get something, compile something and test something regularly. 对于这个简单的任务可能有点过分,但这是获取内容,编译内容和定期测试内容的标准方法。

Eg you could try hudson . 你可以试试哈德森

You should be creating a "mock" interface for your web service that (a) behaves the same way and (b) returns a known answer. 您应该为您的Web服务创建一个“模拟”界面,它(a)的行为方式相同,(b)返回已知答案。

You should then do some other integration testing with the live web service where a person looks at the results and decides if they worked. 然后,您应该使用实时Web服务进行一些其他集成测试,其中一个人查看结果并确定它们是否有效。

Can you only test the generated classes after they were published on the webservice ? 您是否只能在Web服务上发布后测试生成的类? You have no way to test during or just after the generation ? 您无法在生成期间或之后进行测试?

One idea, if the generated code isn't to complex, is to load it via the GroovyClassLoader and to run your tests against it. 如果生成的代码不复杂,一个想法是通过GroovyClassLoader加载它并对其运行测试。 See this page for examples. 有关示例,请参阅此页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM