使用Java中的测试用例更改自动化测试

时间:2021-07-12 14:14:06

I am trying to automate testing for a program written in Java. The problem I have is that there needs to be a way to add/remove more tests cases without changing the code.

我正在尝试自动测试用Java编写的程序。我遇到的问题是需要一种方法来添加/删除更多的测试用例而无需更改代码。

For example, you have two strings and want to check if they are within a list of strings. You pass the the strings over and check. In Java code, I could make an if statement and check if the two strings match some string in a given list. However, if I wanted to add more strings to search for from that list, I would have to go back to the program and add more code.

例如,您有两个字符串,并想检查它们是否在字符串列表中。你将字符串传递过去并检查。在Java代码中,我可以创建一个if语句并检查两个字符串是否匹配给定列表中的某个字符串。但是,如果我想从该列表中添加更多字符串以进行搜索,我将不得不返回该程序并添加更多代码。

Probably a bad example, but I hope you get my point. Of course, there can be many more test cases that might be completely different. And if I wanted to give the program to someone else, they might want to add their own as well.

可能是个坏榜样,但我希望你明白我的意思。当然,可能会有更多的测试用例可能完全不同。如果我想将程序交给其他人,他们可能也想添加自己的程序。

I was thinking to create some kind of template with arguments and method names to call that's outside of the code. Basically a file with rules. The Java code will then interpret what to do with the given rules. I was reading this: https://blog.codecentric.de/en/2016/01/robot-framework-tutorial-2016-keywords/ but wasn't really understanding it.

我想创建一些带有参数和方法名称的模板来调用代码之外的模板。基本上是一个带有规则的文件。然后,Java代码将解释如何处理给定规则。我正在阅读这篇文章:https://blog.codecentric.de/en/2016/01/robot-framework-tutorial-2016-keywords/但是并没有真正理解它。

My goal is to write some generic Java code that can interpret a template file and run the test cases defined in there. Any help would be appreciated, thank you!

我的目标是编写一些通用的Java代码,可以解释模板文件并运行那里定义的测试用例。任何帮助将不胜感激,谢谢!

Example:

Template file

Details:      Checks if String in database
Method:       testID
Arguments:    Hello

Foobar.java

public class Foobar {

    public void testID(String str1)
    {
        // Expected output will be taken from a database
        Assert.assertEquals(str1, expected_output);
    }
}

The String str1 can be taken from the template file under Arguments.

String str1可以从Arguments下的模板文件中获取。

The problem with this is that if I now want to test if str1 is a certain length, I would have to go back and add more code. This would be fine if the program was just for me and my team. However, when you give it to another company or person that doesn't know how to code but they want to run their own tests, it won't be that functional. That's why I was hoping, a person not on the team could just add their test case given they follow the format of the template and the Java program will know what to do with it.

这个问题是,如果我现在想测试str1是否是一定长度,我将不得不返回并添加更多代码。如果该计划仅适合我和我的团队,那就没问题了。但是,当您将其交给另一家不知道如何编码但又想要运行自己的测试的公司或个人时,它就不会那么有用。这就是为什么我希望,一个不在团队中的人可以添加他们的测试用例,因为他们遵循模板的格式,Java程序将知道如何处理它。

I'm sorry I don't know how to explain it that well. Hope it's not confusing.

对不起,我不知道如何解释它。希望它不会混淆。

2 个解决方案

#1


2  

As far as I understand your question,

据我了解你的问题,

You are looking to give a generic framework to whole lot of users where each of them would want it to use it in there own way!

您希望为许多用户提供一个通用框架,其中每个用户都希望它以自己的方式使用它!

My friend this is where "Keyword driven framework" come in to picture!

我的朋友这是“关键字驱动框架”进入图片的地方!

To be more specific as you said you write a function to "check string in a list" but if someone wants to "check the length" then u have to edit your function again!!

更具体的是,你说你写了一个“检查列表中的字符串”的功能,但如果有人想“检查长度”,那么你必须再次编辑你的功能!

All you need to do is analyze the different operations that you have to support. Say, check for string in list, check length of string, check size of list, check string not in list, add string to list etc.

您需要做的就是分析您必须支持的不同操作。比如,检查列表中的字符串,检查字符串的长度,检查列表的大小,检查不在列表中的字符串,将字符串添加到列表等。

So instead of you writing a function with mixture of operations! you make it modular instead and give it back to the users to use it in their own way!

所以不要用混合操作编写函数!你把它变成模块化的,然后把它交给用户以自己的方式使用它!

So if you declare the above 4 keywords (functions). they can call it in the order they want

因此,如果您声明上述4个关键字(函数)。他们可以按照他们想要的顺序调用它

e.g. test cases would then look like:

例如测试用例看起来像:

test1:

check for string in list  <string1>

test2:

check length of string  <string1>
check size of list
check for string not in list  <string1>

You define templates for each of these functions and pass on info to users! so that can use these modularized keywords as they want it. This is how robotframework is done!

您可以为每个功能定义模板,并将信息传递给用户!这样就可以根据需要使用这些模块化的关键字。这就是机器人框架的完成方式!

Hope it is useful!

希望它有用!

#2


1  

There are many ways to create and read test resources for use in Java. You can use straight text strings in .txt files, xml formats, comma delimited formats, etc.

有许多方法可以创建和读取用于Java的测试资源。您可以使用.txt文件,xml格式,逗号分隔格式等直接文本字符串。

It will really depend on the amount and depth of test data that you are trying to inject.

它实际上取决于您尝试注入的测试数据的数量和深度。

There are many questions/answers here that you can search for on how using resource files or other file read methods.

这里有许多问题/答案,您可以搜索如何使用资源文件或其他文件读取方法。

Here is an example: How should I load files into my Java application?

这是一个例子:我应该如何将文件加载到我的Java应用程序中?

#1


2  

As far as I understand your question,

据我了解你的问题,

You are looking to give a generic framework to whole lot of users where each of them would want it to use it in there own way!

您希望为许多用户提供一个通用框架,其中每个用户都希望它以自己的方式使用它!

My friend this is where "Keyword driven framework" come in to picture!

我的朋友这是“关键字驱动框架”进入图片的地方!

To be more specific as you said you write a function to "check string in a list" but if someone wants to "check the length" then u have to edit your function again!!

更具体的是,你说你写了一个“检查列表中的字符串”的功能,但如果有人想“检查长度”,那么你必须再次编辑你的功能!

All you need to do is analyze the different operations that you have to support. Say, check for string in list, check length of string, check size of list, check string not in list, add string to list etc.

您需要做的就是分析您必须支持的不同操作。比如,检查列表中的字符串,检查字符串的长度,检查列表的大小,检查不在列表中的字符串,将字符串添加到列表等。

So instead of you writing a function with mixture of operations! you make it modular instead and give it back to the users to use it in their own way!

所以不要用混合操作编写函数!你把它变成模块化的,然后把它交给用户以自己的方式使用它!

So if you declare the above 4 keywords (functions). they can call it in the order they want

因此,如果您声明上述4个关键字(函数)。他们可以按照他们想要的顺序调用它

e.g. test cases would then look like:

例如测试用例看起来像:

test1:

check for string in list  <string1>

test2:

check length of string  <string1>
check size of list
check for string not in list  <string1>

You define templates for each of these functions and pass on info to users! so that can use these modularized keywords as they want it. This is how robotframework is done!

您可以为每个功能定义模板,并将信息传递给用户!这样就可以根据需要使用这些模块化的关键字。这就是机器人框架的完成方式!

Hope it is useful!

希望它有用!

#2


1  

There are many ways to create and read test resources for use in Java. You can use straight text strings in .txt files, xml formats, comma delimited formats, etc.

有许多方法可以创建和读取用于Java的测试资源。您可以使用.txt文件,xml格式,逗号分隔格式等直接文本字符串。

It will really depend on the amount and depth of test data that you are trying to inject.

它实际上取决于您尝试注入的测试数据的数量和深度。

There are many questions/answers here that you can search for on how using resource files or other file read methods.

这里有许多问题/答案,您可以搜索如何使用资源文件或其他文件读取方法。

Here is an example: How should I load files into my Java application?

这是一个例子:我应该如何将文件加载到我的Java应用程序中?