如何在运行时执行junit测试用例? [英] How to execute a junit testcase at runtime?

查看:102
本文介绍了如何在运行时执行junit测试用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在另一个Java程序中执行一个junit测试用例.

I want to execute a junit testcase inside another java program.. Let's say

class XYZ extends TestCase{
    public void test1(){}
    public void test2(){}
    public void test3(){}
}

就像我在eclipse上运行此单元测试的方式一样,我希望能够在运行时执行此操作.我需要知道通过和失败的测试数量.创建此测试用例类的对象并逐个调用每个函数,但是以某种方式我觉得这样做是无效的.此外,它确实执行了测试,但没有告诉我有关通过和失败的测试统计信息.

Like the way I run this unit test on eclipse, I want to be able to execute this at run time..I need to know the number of tests passed and failed.. I tried the typical java way of doing this by creating an object of this testcase class and invoking each function one by one but somehow I felt it is ineffective to do so.. Also, it does execute the tests but doesn't tell me anything about the statistics about tests passed and failed..

那么我该如何在运行时在另一个Java程序中执行所有这些测试,模仿eclipse的作用呢?

So how can I execute all this tests at run time within another java program, mimicking what eclipse does?

推荐答案

您可以使用 JunitCore 类,以编程方式执行测试.这将返回一个结果,您必须自己对其进行评估.

You can use the JunitCore class to execute tests programmatically. This will return a Result which you will have to evaluate for yourself.

一旦完成测试,就需要运行它们. JUnit提供了以下工具 定义要运行的套件并显示其结果.运行测试 并在控制台上查看结果,请从Java程序运行该结果:

Once you have tests, you'll want to run them. JUnit provides tools to define the suite to be run and to display its results. To run tests and see the results on the console, run this from a Java program:

org.junit.runner.JUnitCore.runClasses(TestClass1.class, ...);

这篇关于如何在运行时执行junit测试用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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