Assert.Matches() 中的预期值和实际值有什么区别
what is the difference between expected and actual values in Assert.Matches()
静态匹配是
public static void Matches(string expectedRegexPattern, string actualString);
传递 expectedRegexPattern
值而不是 actualString
值与传递 actualString
值而不是 expectedRegexPattern
值有什么区别?
方式 1 -Assert.Matches("EN", result[0].LanguageCode);
方式 2 - Assert.Matches(result[0].LanguageCode,"EN");
Both of ways doing same work with same performance. So am confused about the difference between the above ways and which one is best?
我希望您使用 xunit 作为测试框架,因为我在那里找到了那个方法。因此,如果您只想检查两个字符串是否具有相同的内容,您也可以使用 Assert.Equal("Your text", "Your text")
。 Assert.Matches
方法用于验证您的文本是否为所需格式。格式由 .net 正则表达式描述。例如,通过此调用 Assert.Matches("^[0-9]$", "1")
,您可以检查您的字符串是否为 0 到 9 之间的数字。如果将参数的顺序更改为 Assert.Matches( "1", "^[0-9]$")
,该方法将抛出异常并且您的测试将被标记失败了。
@Stivi 正确地指出了 Matches
情况下的显着差异,但即使对于 Equals
和其他断言方法,也存在另一个不那么重要但仍然重要的区别。
您可能会在测试日志输出中收到不正确的消息。许多测试框架会记录一个错误,例如
Values do not match: expected: {expectedValue}, actual: {actualValue}".
因此,如果您切换它们,您会看到错误的 "expected" 值,可能会使试图诊断问题的人感到困惑。
以本次测试为例:
void TestValueIsZero():
{
int value = 1;
Assert.Equals(0, value);
// logs "Values to not match. expected: 0, actual: 1
Assert.Equals(value, 0);
// logs "Values to not match. expected: 1, actual: 0
}
查看失败测试日志的人可能会对由于切换参数导致的日志中不正确的 "expected" 和 "actual" 值感到困惑。
无论参数顺序如何,机制可能完全相同(比较两个对象,如果没有发现差异,return为真),但是语义仍然很重要。
静态匹配是
public static void Matches(string expectedRegexPattern, string actualString);
传递 expectedRegexPattern
值而不是 actualString
值与传递 actualString
值而不是 expectedRegexPattern
值有什么区别?
方式 1 -Assert.Matches("EN", result[0].LanguageCode);
方式 2 - Assert.Matches(result[0].LanguageCode,"EN");
Both of ways doing same work with same performance. So am confused about the difference between the above ways and which one is best?
我希望您使用 xunit 作为测试框架,因为我在那里找到了那个方法。因此,如果您只想检查两个字符串是否具有相同的内容,您也可以使用 Assert.Equal("Your text", "Your text")
。 Assert.Matches
方法用于验证您的文本是否为所需格式。格式由 .net 正则表达式描述。例如,通过此调用 Assert.Matches("^[0-9]$", "1")
,您可以检查您的字符串是否为 0 到 9 之间的数字。如果将参数的顺序更改为 Assert.Matches( "1", "^[0-9]$")
,该方法将抛出异常并且您的测试将被标记失败了。
@Stivi 正确地指出了 Matches
情况下的显着差异,但即使对于 Equals
和其他断言方法,也存在另一个不那么重要但仍然重要的区别。
您可能会在测试日志输出中收到不正确的消息。许多测试框架会记录一个错误,例如
Values do not match: expected: {expectedValue}, actual: {actualValue}".
因此,如果您切换它们,您会看到错误的 "expected" 值,可能会使试图诊断问题的人感到困惑。
以本次测试为例:
void TestValueIsZero():
{
int value = 1;
Assert.Equals(0, value);
// logs "Values to not match. expected: 0, actual: 1
Assert.Equals(value, 0);
// logs "Values to not match. expected: 1, actual: 0
}
查看失败测试日志的人可能会对由于切换参数导致的日志中不正确的 "expected" 和 "actual" 值感到困惑。
无论参数顺序如何,机制可能完全相同(比较两个对象,如果没有发现差异,return为真),但是语义仍然很重要。