There is a test task:

Given the file type

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 is the result of the operation on operand1 and operand2

The file may contain any field values.


Is required

  • Implement unit (JUnit) arithmetic tests.
  • Each action should appear in the report as a separate test script.

End of task


I understand how to write JUnit tests, but I do not understand what exactly is being tested in this case.

Do I think correctly that you first need to write code that parses the variables, 4 functions for different arithmetic operations, and then check with JUnit the correctness of the functions based on the test equalities in the file?

I ask you to express your understanding of the problem.

  • 2
    It is better to clarify this with the one who gave you this task. - newman
  • Thank you for your helpful advice, if I could do this, I would not waste valuable time on site visitors. - SubZr0

2 answers 2

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

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

Well, of course, there are several lines of "binding" for all this, so that the module is "compiled".

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

UPD

Here on the knee on perl do for 5 minutes, for the interview I think the most it

 #!/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"; 
  • KoVadim, thank you. The fact is that we have a java test. If it doesn't bother you, please explain this line :int actual = operand1 operationoperand2; How do you think the operation should be transformed from a character into an operation? With the help of switch and several functions or with the help of some analogue of eval (I undertake to find an analog myself) or in another way? - SubZr0
  • My idea is that a script / program is written that generates a file, which is a ready-made java class, which is then compiled in a standard way and executed. - KoVadim

I would do 4 tests on 1 for each operation (+ - / *)

Each test would select from the file the lines with its operations and check the correctness of the result.

But of course the task is very strange.