Adanay Martín
Adanay Martín

Reputation: 557

AutoFixture UnitTest Parameter Setup

How can I setup AutoFixture for the following unit test:

[Theory, ... ] // <- what goes here? 
public void MyTest(int param1, string param2)
{
  ...
}

The first parameter can take randomly generated integers so AutoFixture naturally fits. The second one cannot be the same way. I need the second one to take the values from a dynamically generated list of values that is not known at compile time. I need to tell that to AutoFixture but I do not know how.

EDIT:

In my specific scenario what I need is that the string param be a property name of some type. I would like to say to AutoFixtue: "Hey, for param2, take a random string from this list where the list is myType.GetPropertyNames().

Does AutoFixture support this scenario?

Upvotes: 2

Views: 353

Answers (1)

Serhii Shushliapin
Serhii Shushliapin

Reputation: 2708

If you need random values, just use [AutoData]:

[Theory, AutoData]
public void MyTest(int param1, string param2)
{
  ...
}

If you need some predefined test data you can use [InlineAutoData]. In the sample below the string parameter param1 will get values specified in the attribute. The int parameter param2 will always be random:

[Theory]
[InlineAutoData("predefined_string_1")]
[InlineAutoData("predefined_string_2")]
public void MyTest(string param1, int param2)
{
  ...
}

Upvotes: 1

Related Questions