Skip to content

Commit

Permalink
. dt Adding documentation for DynamicTest
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottBob committed Feb 28, 2025
1 parent 7676a2b commit 0253dcd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
28 changes: 28 additions & 0 deletions approvaltests/docs/how_to/UseTestFactory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<a id="top"></a>

# How to Use TestFactory

<!-- toc -->
## Contents

* [When to use Approvals::verifyAll()](#when-to-use-approvalsverifyall)
* [Steps](#steps)
* [Tables](#tables)<!-- endToc -->

## Explanation
Approvals uses the stack trace to figure out the name of the `.approved.` file.
Dynamic tests require some intervention to capture this at the right time.

Because of this, you will always have to use the Options when calling `verify()` as well.

## Java
Here is an example of how to do this in Java:
snippet: java_dynamic_test

## Kotlin
Here is an example of how to do this in Kotlin:
snippet: kotlin_dynamic_test

---

[Back to User Guide](/doc/README.md#top)
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
Expand Down Expand Up @@ -123,4 +125,18 @@ Collection<DynamicTest> testFactory2()
}
}));
}

// begin_snippet: java_dynamic_test
@TestFactory
Collection<DynamicTest> testFactory3() {
return Stream.of(1, 2)
.map(number ->
JupiterApprovals.dynamicTest("test " + number,
o -> Approvals.verify("content for " + number, o)
)
)
.collect(Collectors.toList());
}
// end_snippet

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
content for 1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
content for 2
3 changes: 2 additions & 1 deletion mdsnippets.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"ExcludeDirectories": [ "target" ],
"Convention": "InPlaceOverwrite",
"TocLevel": 5
"TocLevel": 5,
"UrlsAsSnippets": ["https://raw.githubusercontent.com/approvals/Approvaltests.Kotlin.StarterProject/refs/heads/main/src/test/kotlin/com/sample/SampleTest.kt"]
}

0 comments on commit 0253dcd

Please sign in to comment.