简体   繁体   中英

Use code generation for executing generic tests

I think I have an interesting question and PERHAPS there is already the answer which is still a secret for me, so I hope to get some helps from expers. :)

So here is the thing:

I work for the test/validation team to test our Java API and basically my job is to follow test plan and write the test code. After writing that for more than two months, I find the codes are really similar. For example:

To test function could return expected result or throw exception correctly, we may need write several .java to run.

1.java set up server connection, connect client and send request, initiate variables with correct values and pass them to the function A, catch the answer and analyse it

2.java set up server connection, connect client and send request, initiate all variables with correct values but one with bad value and pass them to function A, catch the answer and analyse it

3.java set up server connection, connect client and send request, initiate all variables with correct values but two with bad values and pass them to function A, catch the answer and analyse it

so you see in three java test files, the most part of them are the same or similar enough and even copy/paste make the job boring and possible to be wrong.

I wonder whether or not I could define test code corresponding different behavior, then for every test java file, I define a text including the behavior and then a mother class who is in charge of loading the text file and assembling the final test java file according to the text file?

Like this:
Text File:
1) set up server
2) connect client
3) send request
4) initiate variables with correct values
5) initiate variables with correct values but one with bad value
6) initiate variables with correct values but two with bad values
7) catch the result and analyse it

Mother.java
1) load Text file
2) create a son.java
3) find the code corresponding the Text file and write them to son.java

Then the coder open son.java at IDE to check syntax, or import or anything conflict then run it.

Is my idea realizable or not? Is there already something similar?

Any information would be appreciated, thanks a millions in advance!

Honestly, this does not sound like a good use case for code generation. Instead of generating a class for each test case, you should implement a more general testing utility which takes the required input as its data and executes the generic testing code based on this data.

From what you write, this would for example be something like a simple base class for a JUnit test:

abstract class AbstractServerDependantTest {

  protected Server server;

  protected Client client;

  @Before
  public void setUp() {
    server = new Server();
    server.start();
    client = new Client();
    client.connectTo(server);
  }

  @After
  public void tearDpwm() {
    client.disconnect();
    server.shutDown();
  }
}

Now you can write three test classes which inherit from this AbstractServerDependantTest without copy pasting your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM