Repository navigation
Improve unit testing - #660
Conversation
|
Codecov Report
📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more @@ Coverage Diff @@
## main #660 +/- ##
============================================
+ Coverage 92.77% 92.81% +0.03%
- Complexity 2634 2635 +1
============================================
Files 283 283
Lines 5426 5426
Branches 589 589
============================================
+ Hits 5034 5036 +2
- Misses 238 241 +3
+ Partials 154 149 -5
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
|
what do you think about parameterized test on |
|
Can you provide params to a Or maybe I'm misunderstanding your vision. |
|
It seems Here's what I tried. Click the triangle/control to the left to expandpackage net.datafaker.providers.base;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
abstract class AbstractProviderTest extends BaseFakerTest<BaseFaker> {
@SuppressWarnings("unchecked")
protected List<String> getBaseList(String key) {
return (List<String>) faker.fakeValuesService().fetchObject(key, faker.getContext());
}
abstract Stream<Arguments> simpleTestArgs();
/**
* Ensures that the supplied method actually returns a value in the expected list.
*
* @param method the method from which the test value will be obtained.
* @param key the YAML key from which the list of actual items should be retrieved.
*/
@ParameterizedTest
@MethodSource("simpleTestArgs")
protected void simpleTest(Supplier<?> method, String key) {
System.out.println("Method: " + method == null);
System.out.println("Key: " + key == null);
// Given
List<String> actual = getBaseList(key);
// When
String item = (String) method.get();
// Then
assertThat(actual).anyMatch(item::equals);
}
}package net.datafaker.providers.base;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.provider.Arguments;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.stream.Stream;
class ApplianceTest extends AbstractProviderTest {
@Test
void brand() {
assertThat(faker.appliance().brand()).matches("[A-Za-z .-]+");
}
@Override
Stream<Arguments> simpleTestArgs() {
return Stream.of(
Arguments.of(faker.appliance().equipment(), "appliance.equipment"));
}
} |
|
it could be non-static however in this case @TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class BaseFakerTest<T extends BaseFaker> {
...
@ParameterizedTest(name = "{0}")
@MethodSource("simpleTestArgs")
protected void simpleListTest(TestSpec testSpec) {
// Given
List<String> actual = getBaseList(testSpec.key);
// When
String item = (String) testSpec.supplier.get();
// Then
assertThat(actual).anyMatch(item::equals);
}
protected abstract Collection<TestSpec> simpleTestArgs();
protected static class TestSpec {
private final Supplier<?> supplier;
private final String key;
private TestSpec(Supplier<?> supplier, String key) {
this.supplier = supplier;
this.key = key;
}
public static TestSpec of(Supplier<?> supplier, String key) {
return new TestSpec(supplier, key);
}
@Override
public String toString() {
// The result of this toString will be used by IDE in test report
return "Key: " + key;
}
}
...It's better to use pojo since there are some java compiliation issues for the case like On child tests it could look like that e.g. just one method instead of 3 in class AustraliaTest extends BaseFakerTest<BaseFaker> {
@Override
protected Collection<TestSpec> simpleTestArgs() {
return Arrays.asList(TestSpec.of(() -> faker.australia().locations(), "australia.locations"),
TestSpec.of(() -> faker.australia().animals(), "australia.animals"),
TestSpec.of(() -> faker.australia().states(), "australia.states"));
}
} |
|
Cool thanks!! I’ll tweak the names too, I know all the simple* weren’t great. I was just trying to get a functioning model setup. |
|
I had to separate it into another class so that it could be extended by only those classes where it was applicable. But it's working nicely so I'm moving ahead. I'm about a third done the base provider list. Will get this updated later this evening. |
95679ea to
c7b24b9
Compare
|
The LocalityTest failure seems to be related to having added |
c7b24b9 to
6bd8f0f
Compare
|
Relocated it to the new Abastract which resolved the issue. |
6bd8f0f to
6d152a8
Compare
|
Thanks, it looks great. |
6d152a8 to
e077307
Compare
e077307 to
9921f5c
Compare
- AbstractBasicProviderTest > Added so that applicable tests could extend it in order to ensure that the values the providers return are actually within the expected list. - various > Updated Unit Test classes to leverage the new abstract. - Overwatch > moved to the videogame package. Signed-off-by: kingthorin <kingthorin@users.noreply.github.com>
9921f5c to
e370e09
Compare
|
This PR looks @kingthorin . If you think it's okay, I'm happy if you want to merge it yourself. |
|
PS: I'm not sure if it's too much to ask, but there's a |
|
I’ll have a look at that. If it’s quick I’ll add it here, otherwise I’ll tackle it in another PR. |
No need to tackle it, I can do it, and I'm not even sure we still need the class since we now have Providers for almost all yml files, except for the gaming ones, the are slightly more complex to auto generate. |
|
@bodiam There's one pending review. Reading back through the log: you self requested a review after you suggested merging? 😉 I’m going to guess stray click but want to be sure before I go ahead with it. (Thanks for the collab invite BTW) |
Ah, I was in two minds here, I wasn't sure if we required a review of not, and then I was reviewing, and got distracted :) So let's call it a stray click indeed :) |
|
Yay 🎉 |
|
Woohoo! Nice! 🥇 |
This is in follow-up to: #657.
Signed-off-by: kingthorin kingthorin@users.noreply.github.com