我正在學習使用單元測試,我創建了一個項目,添加了xunit參考。 而以下代碼:爲什麼xunit不允許測試帶參數的方法?
namespace UnitTestProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[Fact]
private void test(int number1, string number2)
{
int result = number1 + Convert.ToInt32(number2);
Assert.IsType(Type.GetType("Int32"), result);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
當我使用的xUnit GUI工具運行測試,它表示:
UnitTestProject.Form1.test信息:System.InvalidOperationException:事實 方法UnitTestProject.Form1.test不能有參數堆棧跟蹤: 於Xunit.Sdk.FactCommand.Execute(對象識別TestClass)
Xunit.Sdk.FixtureCommand.Execute(對象識別TestClass)
Xunit.Sdk.BeforeAfterCommand.Execute(對象識別TestClass)
Xunit.Sdk.LifetimeCommand.Execute(對象識別TestClass)
Xunit.Sdk.ExceptionAndOutputCaptureCommand.Execute(對象識別TestClass)
那麼,怎樣才能我測試用的參數的方法/功能?
謝謝,但我可以將InlineData設置爲隨機值嗎?或者更好地說,InlineData接受內聯方法/變量? – Cheung
你可以爲什麼不。看到下面的一個髒例子。 – Alina
@Shiba一般你不應該在測試中使用隨機值。在您的示例方法中,如果一個隨機數字失敗,您將不知道導致問題的數字是什麼。儘量使測試具有確定性,並提供一系列預期的,意外的和極端的輸入。 – DaveShaw