0

I'm using Xunit to run some Selenium tests. I want to parametrize some tests but having some difficulties.

I have a class that has the locaters for elements stored as properties "IssuesLogUtils"

public class IssuesLogUtils
{
    // Page Heading
    public By page_heading { get; set; } = By.Id("ctl00_lblPageTitle");

    public By issue_title { get; set; } = By.Id("ctl00_ContentPlaceHolder1_txtNonComplianceName");

I then have my Xunit tests in another TestClass file. I want to just use the properties from IssuesLogUtils as params in my tests.

e.g.

 [Theory]
    [InlineData(issues_log_utils.ifco_radial)]
    [InlineData(issues_log_utils.issue_title)]

I have found some articles online that point at really convoluted ways of doing this. Is there any straight forward way? I don't want to have to create a list of params which then the list gets passed in.

Progman
  • 16,827
  • 6
  • 33
  • 48
agleno
  • 388
  • 4
  • 17
  • No it doesn't @Fabio. That's asking me to create a class that returns an object with my params. The InlineData allows me to write int/string as a param very simply. But Xunit won't let me use a property as a param. The property itself could just be a string, but it won't let me use that without me creating some convoluted class with my params. – agleno Feb 12 '21 at 11:28

1 Answers1

2

But Xunit won't let me use a property as a param

This is not XUnit fault. InlineData attribute values must be "known" at compile time, similar to constants.
Properties of some class instances are runtime values and cannot be used for constants or attribute values.

Notice that you don't need to create a class you can use static method with MemberData attribute, which you can put beside the test method, so you can easily see it when reading the test method.

public static IEnumerable<object[]> MyIssuesData()
{
    var issues = new IssuesLogUtils();
    yield return new object[] { issues.page_heading  };
    yield return new object[] { issues.issue_title  };
}

[Theory]
[MemberData(nameof(MyIssuesData))]
public void Should_do_nothing(By issue)
{
     var result = DoSomethingWith(issue);

     result.Should().BeEmpty();
}
Fabio
  • 31,528
  • 4
  • 33
  • 72
  • Super! That worked almost perfectly, thanks @Fabio. Slight issue is that test explorer is not picking up on the params as separate tests and they are run in series and not in parallel. I wonder would it work if I return a list of objects maybe? – agleno Feb 15 '21 at 09:37
  • @agleno, all tests within same test class are executed in a sequence. – Fabio Feb 15 '21 at 20:05