Nó có thể được thực hiện bằng cách sử dụng Thuộc tính TestFixture với tham số.
Nếu tất cả các bài kiểm tra trong lớp phụ thuộc vào cùng một tham số thì đây là cách để thực hiện.
Lớp học sẽ cần một hàm tạo với cùng tham số được chuyển đến thuộc tính TestFixture.
Lịch thi đấu
Xem tham số thử nghiệm trên https://github.com/nunit/docs/wiki/TestFixture-Attribute
[TestFixture("Oscar")]
[TestFixture("Paul")]
[TestFixture("Peter")]
public class NameTest
{
private string _name;
public NameTest(string name)
{
_name = name;
}
[Test]
public void Test_something_that_depends_on_name()
{
//Todo...
}
[Test]
public void Test_something_that_also_depends_on_name()
{
//Todo...
}
//...
}
Mã này là từ trang web tài liệu nunit:
[TestFixture("hello", "hello", "goodbye")]
[TestFixture("zip", "zip")]
[TestFixture(42, 42, 99)]
public class ParameterizedTestFixture
{
private readonly string eq1;
private readonly string eq2;
private readonly string neq;
public ParameterizedTestFixture(string eq1, string eq2, string neq)
{
this.eq1 = eq1;
this.eq2 = eq2;
this.neq = neq;
}
public ParameterizedTestFixture(string eq1, string eq2)
: this(eq1, eq2, null)
{
}
public ParameterizedTestFixture(int eq1, int eq2, int neq)
{
this.eq1 = eq1.ToString();
this.eq2 = eq2.ToString();
this.neq = neq.ToString();
}
[Test]
public void TestEquality()
{
Assert.AreEqual(eq1, eq2);
if (eq1 != null && eq2 != null)
Assert.AreEqual(eq1.GetHashCode(), eq2.GetHashCode());
}
[Test]
public void TestInequality()
{
Assert.AreNotEqual(eq1, neq);
if (eq1 != null && neq != null)
Assert.AreNotEqual(eq1.GetHashCode(), neq.GetHashCode());
}
}
Nguồn
2014-10-06 17:13:37
http://www.nunit.org/index.php?p=parameterizedTests&r= 2.5.5 – Oded
Không rõ tại sao bạn cố gắng tham số hóa * thiết lập * thay vì * kiểm tra *. –
sau khi thiết lập này các bài kiểm tra làm cùng một công việc. nó có thể có ý nghĩa hơn để đưa bit này vào thử nghiệm và làm cho các thử nghiệm parametrized! – dushyantp