Replies: 4 comments
-
Hi @mrpmorris, For these kind of scenarios I'd usually recommend generating the variations elsewhere and then inject them into the current test case. For example, it should be possible to use a separate method to generate the flags object, and then inject it in other services that might use it. [Theory]
[MemberAutoData(nameof(FeatureFlagsVariations))]
public void CanGenerateAllPossibleVariations([Frozen] AllPossibleFlagsForThisTest flags, SomeService service1)
{
// Your test code here
}
public static IEnumerable<object[]> FeatureFlagsVariations()
{
// Generate all possible combinations of flags
yield return [new AllPossibleFlagsForThisTest { /* ... */ }];
} Could you provide more details on this utility that you're using at the moment? Perhaps a snippet of code showing how you're currently implementing the tests, and how do you see AutoFixture helping with that. |
Beta Was this translation helpful? Give feedback.
-
Hi Andrei, thanks for your quick response! My company is using feature flags. I could simply write a test that says But then when someone else is working in the same area and has their own feature flag, I don't want to risk my code breaking because their code did something they didn't think to check for. I wrote some code like this. IEnumerable<MyType> allVariations = VariationsBuilder.Create<MyTable>()
.With(x => x.Enabled, [true, false])
.With(x => x.Obsolete, [true, false])
.With(x => x.DeliveryLeadDays, [1, 3, 5, 7])
.Build(); I would then loop through each This morning I was informed of public class UnitTest1
{
[Theory, CombinatorialData]
public void Test1(
[CombinatorialValues] FlagsCombination flags)
{
}
[Theory, CombinatorialData]
public void Test2(
[CombinatorialValues] FlagsCombination flags,
[CombinatorialValues(null, "", " ")] string inputValue)
{
}
public record struct FlagsCombination(bool Is520Enabled, bool Is123Enabled);
} I thought my approach might be a nice addition to AutoFixture so offered to add it. Although I no longer need it just for flag combinations I thought it might be useful for building data like this IEnumerable<Person> = fixture.CreateVariations<Person>()
.With(x => x.GivenName, ["Bob", "Peter", "Fred"])
.With(x => x.FamilyName, ["Monkhouse", "Pan", "Flintstone"])
.With(x => x.Age, [5, 25, 55]); Maybe for building large data sets or something? |
Beta Was this translation helpful? Give feedback.
-
@mrpmorris can you provide the code of the variations generator? |
Beta Was this translation helpful? Give feedback.
-
The code I shared was the interface to consume the code rather than the code itself. I got chat gpt to write something that was "good enough for now", but I had an idea for a better implementation that I intended to write in my own time. After posting here I got impatient and wrote my better idea whilst working for them, so now I'll have to ask if they'd be willing to contribute it. I'll let you know what they say. I'm sorry for my impatience :) |
Beta Was this translation helpful? Give feedback.
-
Description
We are using feature flags where I work. My tests need to test various combination of those flags.
Example
Imagine I have 3 stories in my system that all affect the same class, and therefore my unit tests.
In my unit tests project I might create a class for this
and then iterate over them
IEnumerable flagVariations = fixture.Create()
.WithVariations(x => x.IsFeature123Enabled_DisallowPurchaseOfDiscontinuedProducts, [true, false])
.WithVariations(x => x.IsFeature234Enabled_UseNewPaymentsGateway , [true, false])
.WithVariations(x => x.Feature345_SpecialOfferDiscountPercent , [0m, 0.2m, 0.6m])
.Build();
Chat GPT put something together for me, but it's overly compllicated and therefore difficult to understand. I'd like to write an implementation myself from scratch so it is more easily understood. They should then be able to switch to the (better) AutoFixture solution.
Tasks
More Information
If I can work out your code base (I've not yet looked) and possibly with your guidance then I can submit a PR.
Beta Was this translation helpful? Give feedback.
All reactions