使用java的测试用例生成器

发布于 2024-12-09 11:45:50 字数 159 浏览 0 评论 0原文

我正在使用 java 开发一个“自动测试用例生成器”。 java 程序的输入将由 prolog 程序提供。例如,如果输入是整数 2,那么 java 程序应该对数字进行平方并将其显示为输出。以同样的方式,如果有 3 个整数,java 程序应该一次接受一个数字并显示所有结果(我的意思是它应该测试每种情况)。

I'm developing an 'Automatic test case generator' using java. The inputs for the java program will be fed by prolog program. If the input is for example an integer 2 then the java program should square the number and display it as output. In the same way if there are 3 integers the java program should accept one number at a time and display all results (i mean it should test each case).

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

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

发布评论

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

评论(1

情泪▽动烟 2024-12-16 11:45:50

正如我在评论中所说,所有输入都是字符串。程序必须将字符串转换为不同的对象类型以“测试每种情况”,例如在以下程序中。

import java.util.*;

public class CaseTester {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in).useDelimiter(" "); //separate entries with a space
        String input;
        while (sc.hasNext()) {
            input = sc.next();
            try {
                double num = Double.parseDouble(input);
                System.out.println("" + Math.pow(num, 2));
            } catch (NumberFormatException e) {
                //input was not a number so move to the next "test"
            }
            try {
                URL test = new URL(input);
                System.out.println("Valid URL");
            } catch (MalformedURLException e) {
                //input was not a valid URL so move to the next "test"
            }
            //put more tests here if you want
        }
        sc.close();
    }
}

以防万一您感到困惑,您并不总是需要使用 try 块进行测试。您也可以使用 ifswitch 块(即 if (input.equalsIgnoreCase("dog")) //do Something

As I said in a comment, all inputs are Strings The program has to convert the strings into different object types to "test each case", such as in the following program.

import java.util.*;

public class CaseTester {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in).useDelimiter(" "); //separate entries with a space
        String input;
        while (sc.hasNext()) {
            input = sc.next();
            try {
                double num = Double.parseDouble(input);
                System.out.println("" + Math.pow(num, 2));
            } catch (NumberFormatException e) {
                //input was not a number so move to the next "test"
            }
            try {
                URL test = new URL(input);
                System.out.println("Valid URL");
            } catch (MalformedURLException e) {
                //input was not a valid URL so move to the next "test"
            }
            //put more tests here if you want
        }
        sc.close();
    }
}

Just in case you're confused, you don't always have to conduct tests with a try block. You can use if and switch blocks as well (i.e. if (input.equalsIgnoreCase("dog")) //do something)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文