4

I have a method which generates a few images (1.jpg, 2.jpg...) writing them to the file system. I want to verify the results of this method with ApprovalTest. The problem is that Approvals.verify(image) names the received and approved files as the test method is named. So I cannot verify more than one image with one test.

How could I verify multiple images in one test?

Andrey Minogin
  • 4,521
  • 6
  • 38
  • 60

2 Answers2

6

You can use NamerFactory to change information appended to the end of a file. For next test.

[TestFixture]
class Program
{
    [Test]
    [UseReporter(typeof(WinMergeReporter))]
    public void Test1()
    {
        var image1 = @"firstImage.png";
        var image2 = @"secondImage.png";

        NamerFactory.AdditionalInformation = Path.GetFileNameWithoutExtension(image1);
        ApprovalTests.Approvals.Verify(image1);

        NamerFactory.AdditionalInformation = Path.GetFileNameWithoutExtension(image2);
        ApprovalTests.Approvals.Verify(image2);
    }
}

Approval Tests has created two files with firstImage and secondImage before end. See the screenshot for clarity:

enter image description here

My objects are strings, but for your images all would be the same. You would call Approvals.Verify(image) as before, but just changing AdditionalInformation as in the example.

Note: I would not quite recommend verifying two images in one test, because if one Verify will fail - next verifies won't be executed. And there is no way for Approval Tests to concatenate images and verify them in one step, at least if you do this on your own.

Edit: for Java try to use, located at NamerFactory

public static void asMachineSpecificTest(Function0<String> environmentLabeller)
{
    additionalInformation = environmentLabeller.call();
}

and provide appropriate Function, that will return names of your images

Ilya Ivanov
  • 23,148
  • 4
  • 64
  • 90
  • I forgot to mention I am using Java. Will this work with Java as well? – Andrey Minogin Jan 06 '13 at 14:23
  • I think yes, API is the same. Give it a try and respond here about the results. – Ilya Ivanov Jan 06 '13 at 14:29
  • In Java version org.approvaltests.namer.NamerFactory#additionalInformation is private and has no setter. – Andrey Minogin Jan 07 '13 at 10:49
  • ok, I will investigate it by myself, thanks for response. What version of Approval Tests do you use? – Ilya Ivanov Jan 07 '13 at 10:50
  • I am using 0.12 http://sourceforge.net/projects/approvaltests/files/ApprovalTests.Java/ – Andrey Minogin Jan 07 '13 at 10:56
  • are there `AsMachineSpecificTest` public method in `NamerFactory`? Installing eclipse so it could take time. – Ilya Ivanov Jan 07 '13 at 10:58
  • Yes, it is here and it can be used to rename test result, though it is not that convenient. Please edit your answer to include this idea. – Andrey Minogin Jan 07 '13 at 11:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22309/discussion-between-andrey-and-ilya-ivanov) – Andrey Minogin Jan 07 '13 at 11:16
  • Prior to finding this answer, I was creating the 'gold version' file by placing the contents of a pdf into a FileStream object, creating the *.approval 'gold version', testing for approval, and then trying to delete the file again before the next 'test_file' was checked/creating a new *.approval 'gold version'. I thought the 'name' of the *.approved file wasn't configurable, until now. I was having trouble with the timing of this because the process creating the file still had a handle on it. This solution is far more along the lines of what I wanted, thank you. – Brien Foss Mar 25 '15 at 01:05
  • @BrienFoss I'm very glad I was able to help. – Ilya Ivanov Mar 25 '15 at 10:26
3

Ilya's answer is correct, but it's slightly different in java, so I wanted to post a full sample here:

package org.approvaltests.namer.tests;

import junit.framework.TestCase;

import org.approvaltests.Approvals;
import org.approvaltests.namer.NamerFactory;
import org.lambda.functions.Function0;

public class NamerFactoryTest extends TestCase
{
  public static class MultipleFiles implements Function0<String>
  {
    private int count = 1;
    @Override
    public String call()
    {
      return "" + (count++);
    }
  }
  public void testMultipleFiles() throws Exception
  {
    MultipleFiles f = new MultipleFiles();
    NamerFactory.asMachineSpecificTest(f);
    Approvals.verify("one");
    NamerFactory.asMachineSpecificTest(f);
    Approvals.verify("two");
  }
}

ps. I'll add this to the next release so it will just be

package org.approvaltests.namer.tests;

import junit.framework.TestCase;

import org.approvaltests.Approvals;
import org.approvaltests.namer.MultipleFilesLabeller;
import org.approvaltests.namer.NamerFactory;

public class NamerFactoryTest extends TestCase
{
  public void testMultipleFiles() throws Exception
  {
    MultipleFilesLabeller labeller = NamerFactory.ApprovalResults.useMultipleFiles();
    Approvals.verify("one");
    labeller.next();
    Approvals.verify("two");
  }
}
llewellyn falco
  • 2,281
  • 16
  • 13