【问题标题】:Running a Unit test on a separated code在单独的代码上运行单元测试
【发布时间】:2014-04-19 12:13:26
【问题描述】:

我不知道如何解释它,但我会尽力而为。

我有这个项目,客户希望我创建一个比赛。一个人必须上传他的 Java 代码,另一个人必须上传他的 JUnit 测试。 我的应用程序应该获取这 2 个文件并在上传的代码上运行上传的测试。

现在我知道如何让用户上传他们的文件,但我一点也不知道如何实际运行测试。谁能帮我解决这个问题?

这是我目前得到的代码:

public void actionPerformed(ActionEvent e) 
{
    if (e.getSource() == codeB)
    {
        //Get the code
        File dir = new File("..");
        fc.setCurrentDirectory(dir);
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == fc.APPROVE_OPTION)
        {
            codeF = fc.getSelectedFile();
            codeV.setText(codeF.getPath());
        }
        else
        {
            System.out.println("Cancel");
        }
    }

    if (e.getSource() == testB)
    {
        //Get the test
        File dir = new File("..");
        fc.setCurrentDirectory(dir);
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == fc.APPROVE_OPTION)
        {
            testF = fc.getSelectedFile();
            testV.setText(testF.getPath());
        }
        else
        {
            System.out.println("Cancel");
        }
    }

    if (e.getSource() == run)
    {
        //Run the tests on the code
    }

【问题讨论】:

标签: java unit-testing junit automated-tests


【解决方案1】:

您的代码 sn-p 与您的问题完全无关。

你要做的是:

  1. 编译类
  2. 编译单元测试类
  3. 在同一个类加载器中加载两者
  4. 执行测试

为了以编程方式执行 junit 测试,您必须这样做:

JUnitCore junit = new JUnitCore();
Result result = junit.runClasses(testClasses); // testClasses is the array of test classes to be executed

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多