java – How would you summarize the formulation of a test problem?

Question:

There is a test task:

Given a view file

 operand1;operand2;operation;result operand1;operand2;operation;result operand1;operand2;operation;result operand1;operand2;operation;result

Each line describes an arithmetic operation.

  • operand1 and operand2 – operands, integers

  • operation – arithmetic operation + – / *

  • result – result of operation operation on operand1 and operand2

The file can contain any field values


Required

  • Implement JUnit tests for arithmetic operations.
  • Each action should appear in the report as a separate test case

End of the quest


I understand how to write JUnit tests, but I don't understand what exactly is being tested in this case.

Am I right in thinking that you need to first write a code that parses variables, 4 functions for different arithmetic operations, and then use JUnit to check the correctness of the functions based on test equalities in the file?

I ask you to express your understanding of the problem.

Answer:

I would write a script (although if there are several lines, then it is possible with pens), which, based on the source file, will generate a Java file with tests. That is, for each line of the source file, it will generate something like

@Test
public void test1() {
    int actual = operand1 operationoperand2;
    int expect = result;
    assertEquals(expect , actual);
}

well, of course, a few lines of "binding" for all this, so that the module would be "compiled".

In a more sophisticated way, I would add a check to 0 for operand2 if operation is / .

UPD

Here on my knees in perl to do in 5 minutes, for an interview I consider it the most

#!/usr/bin/perl

use strict;
use warnings;

print '
import junit.framework.*;

public class JavaTest extends TestCase {

protected void setUp(){

}
';

my $i = 1;
while (my $line = <>) {
    chomp $line;
    my ($op1, $op2, $oper, $result) = split /;/, $line;
    print <<"ONE_TEST";
\@Test
public void test$i() {
    int actual = $op1 $oper $op2;
    int expect = $result;
    assertEquals(expect, actual);
}

ONE_TEST

$i += 1;
}

print "}\n";
Scroll to Top