質問

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?

役に立ちましたか?

解決

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'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");
  }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top