-1

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.

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.

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.

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!

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.

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.

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

Vylic
  • 69
  • 2
  • 9

2 Answers2

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

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!

Waman
  • 1,179
  • 7
  • 16
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.

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?

Community
  • 1
  • 1
David D
  • 36
  • 5