הקדמה
במאמר זה, אסקור תכונה חדשה יחסית ב-NUnit 3 - סביבת בדיקות ל-C#. תכונה זו נקראת “Multiple Asserts” (אימותים מרובים).
הכלים המשמשים להדגמת הדוגמאות הבאות הם:
- Visual Studio 2017
- NUnit 3
- Selenium WebDriver
למה שנרצה להשתמש ב-assertions מרובים?
- פחות קוד בדיקה.
- assertions מרובים הם הרבה יותר מהירים.
- לפני תכונה זו, היינו צריכים לכתוב שני מקרי בדיקה שמאמתים חלק אחר או לכתוב את ה-assertions בזה אחר זה. במקרה שישנם assertions מרובים ואחד נכשל, ה-assertion שאחרי ה-assertion שנכשל לא ירוץ, בניגוד לתכונה שמאמתת הכל!
המצב הקודם:
[Test, Description("Checks for URL title")]
public void TestTitle() {
Assert.That(driver.Title, Is.EqualTo("Not Google"));
}
[Test, Description("Checks for URL")]
public void TestURL() {
Assert.That(driver.Url, Is.EqualTo("http://www.google.com/"));
}
באמצעות assertions מרובים, הנה התוצאה שאחרי:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace NUnit.Tests1 {
[TestFixture]
public class TestClass {
IWebDriver driver;
[SetUp]
public void BeforeTestMethod() {
driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.com/");
}
[Test, Description("Checks for title and URL")]
public void TestTitleURL() {
Assert.Multiple(() => {
Assert.That(driver.Title, Is.EqualTo("Not Google"));
Assert.That(driver.Url, Is.EqualTo("http://www.google.com/"));
});
}
[TearDown]
public void AfterTestMethod() {
driver.Quit();
}
}
}
כאשר הבדיקה נכשלת - כל ה-assertions שנכשלו נצברים למקום אחד:
Test Name: TestMethod
Test Outcome: Failed
Test Duration: 0:00:10.604
Result1 Name: TestTitleURL
Result1 Outcome: Failed
Result1 Duration: 0:00:05.302
Result1 Message:
Expected string length 10 but was 6. Strings differ at index 0.
Expected: "Not Google"
But was: "Google"
----------^
Result2 Name: TestTitleURL
Result2 Outcome: Failed
Result2 Duration: 0:00:05.302
Expected string length 22 but was 34. Strings differ at index 4.
Expected: "http://www.google.com/"
But was: "https://www.google.com/?gws_rd=ssl"
----------------^
לסיכום
במאמר זה, סקרנו את תכונת ה-assertions המרובים של NUnit 3 ואת יתרונותיה. קריאה נוספת על assertions מרובים בקישור זה.
בדיקות מהנות!