I have a data driven test in C# that verifies a function properly creates a string given an input.
Code under test:
public static class Formatter
{
public static String FormatWithCarriageReturn(int position)
{
return String.Format("#0 P{0} T1000 \r", position);
}
}
The FormatWithCarriageReturn
must return the string with the carriage return appended.
In order to test this method, I created a data driven test in MSTest that loads data from a CSV file.
CSV File:
position, expected
1, #0 P1 T1000 \r
2, #0 P2 T1000 \r
3, #0 P3 T1000 \r
4, #0 P4 T1000 \r
5, #0 P5 T1000 \r
6, #0 P6 T1000 \r
Test Method:
/* Test method and data source attributes*/
public void FormattingWithCarriageReturnFormatsCorrectly()
{
String expected = TestContext.DataRow["expected"].ToString();
/* various setup code. */
int requestedPosition = Convert.ToInt32(TestContext.DataRow["position"]);
String actual = Formatter.FormatWithCarriageReturn(requestedPosition);
Assert.AreEqual(expected, actual);
}
The problem is the TestContext.DataRow["expected"].ToString()
statement will load the string literal #0 P1 T1000 \\\\r
, and the Assert
fails because it is comparing #0 P1 T1000 \\r
with #0 P1 T1000
<- I can't get this to format correctly. There should be a carriage return at the end.
Is there a way to get the expected string out of the CSV file with the carriage return in place? I could list the expected string in the CSV file as #0 P1 T1000
, and then do something like expected = String.Concat(expected, " \\r");
, but I'd like to keep the full expected string in the CSV file.
Edit: Here's the current Assert message that I'm getting:
Result1 Message: Assert.AreEqual failed. Expected:<#0 P1 T1000 \r>. Actual:<#0 P1 T1000
>.
A passing message would read
Result1 Message: Assert.AreEqual passed. Expected:<#0 P1 T1000
>. Actual:<#0 P1 T1000
>.
Note that both expected
and actual
have the carriage return at the end.
Remove the "\\r" from the lines in the csv file and change
String expected = TestContext.DataRow["expected"].ToString();
to
String expected = TestContext.DataRow["expected"].ToString() +
new String(new[] { (char)0x0D });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.