0

I have a controller that outputs a CSV file after some processing :

Some records are taken from a MySQL table then processed (some cleaning and aggregation), then converted to CSV file.

Is it correct to test with something like this :

$this->assertSame(file_get_contents('myCsv.csv'), $expectedString);

It works but I modified many times $expectedString, sometimes even after changing a specific labelor name.

1
  • 1
    It depends what you want to test. In your case you assert that the content of the file will be exactly the same, so you have to modify $expectedString each time something changes. You may read the file and test specific rows only. Really depends on what you're building. BTW assertSame should have $expectedString as first parameter. Commented Jul 31, 2015 at 10:37

1 Answer 1

1

The whole idea of unit testing is to have a predefined value and to define what the method should return after processing.

This makes sense:

$n = add(1, 1);
$this->assertEquals(2, $n);

This does not:

$rand = generateRandomNumber();
$this->assertIsRandom($rand); // What does this even mean?

This means that your test needs to have a .cvs-file that is the same for every test, and the data from the database is the same every time. If you have this you can calculate what the expected results should be. If there are any variables outside the test that can vary, reliable testing is impossible.

A better approach is to split the test in multiple smaller modules. Test the processing, cleaning and aggregation that the final content is manipulated by. This is easily testable. For example:

$str = removeUpperCase('abCDe');
$this->assertEquals('abe', $str);
Sign up to request clarification or add additional context in comments.

3 Comments

What I have done is to build manually the CSV file by doing the processing myself then check the generated CSV file with my manual CSV. But it's a little bit laborious because I have to check the expected file each time some chanes are made. May be it would be better if I just test some portions of CVS file that are affected by the logic (for example an aggregated column).
There should be no changes that affects the final result. That is the meaning of testing. Test without the aggregated columns or do other testing on it. That is the point I am trying to make here. There should not be any changes to the test data that require changes to the expected result. That should not be included in the testing.
If you are bound to random values, you should not test with absolute values and rather use regular expressions. Its not a good practice to regular update expected values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.