Passing a String for an Attribute Attribute by a Call Method

I try to use NUnit and pass a string argument to the TestCase attribute, but I get "The attribute argument must be a constant expression, a type expression, or an array expression of the attribute parameter type"

This is a simplified version, but MyStatic is a call that returns the RegEx string built, so every method in MyStatic, which is called adding to a string constructor, has an implicit conversion to a string.

I want to keep this method, because if I create separate unit tests, I will go against the principle of DRY.

  [TestCase("","/123",MyStatic.DoThis().And().GetString("ABC"), "id","123")]
  public void MyMehthod(string Root, string Path, string Route, string Param, string Expected)
  {
    var result = SetupRouteResponse(Root, Path, Route, "MatchIt");

    Assert.AreEqual(Expected, (string)result.Context.Parameters[Param]);
  }
+3
source share
1 answer

TestCaseSource : http://www.nunit.org/index.php?p=testCaseSource&r=2.5.9

:

 [Test, TestCaseSource("DivideCases")]
 public void DivideTest(int n, int d, int q)
 {
    Assert.AreEqual( q, n / d );
 }

 static object[] DivideCases =
 {
    new object[] { 12, 3, 4 },
    new object[] { 12, 2, 6 },
    new object[] { 12, 4, 3 } 
 };

:

 [Test, TestCaseSource("MyCaseSource")]
 public void MyMehthod(string Root, string Path, string Route, string Param, string Expected)
 {
   var result = SetupRouteResponse(Root, Path, Route, "MatchIt");

   Assert.AreEqual(Expected, (string)result.Context.Parameters[Param]);
 }

 static object[] MyCaseSource=
 {
    new object[] { "","/123",MyStatic.DoThis().And().GetString("ABC"), "id","123" },
 };
+10

All Articles