簡體   English   中英

我是否正確使用單元測試?

[英]Am I using Unit Testing correctly?

這是我第一次編寫TestMethod。 我是否正確使用單元測試?

[TestMethod]
public void TestUpdateAccount()
{
    GwIntegrationServiceSoapClient client = new GwIntegrationServiceSoapClient();

    int id = 21; // Target account to update.
    Account accountToUpdate = client.ReadAccount(id);

    string oldName = accountToUpdate.Name;
    string oldEmail = accountToUpdate.Email;
    string newName = Guid.NewGuid().ToString();
    string newEmail = Guid.NewGuid().ToString() + "@nomail.com";

    // Update the name only, even a value is passed in newEmail 
    // But in the propertiesToUpdate parameter (last one) only Name value is passed.
    // Note: the UpdateAccountProperty could be: 
    //          [UpdateAccountProperty.Name | UpdateAccountProperty.Name.Email]
    client.UpdateAccount(id, newName, newEmail, null, false, UpdateAccountProperty.Name);

    // Read the record after updating from database
    Account updatedAccountFirstTime = client.ReadAccount(21);

    // The name should be changed
    Assert.AreEqual(newName, updatedAccountFirstTime.Name);

    // The email should not be changed
    Assert.AreNotEqual(newEmail, updatedAccountFirstTime.Email);

    ////////////////////////////////////////////////////////////
    // Now, after updating the name and everything is working well
    // Returning the old name to the record.
    client.UpdateAccount(id, oldName, oldEmail, null, false, UpdateAccountProperty.Name);

    // Read the record after updating
    Account updatedAccountSecondTime = client.ReadAccount(21);

    Assert.AreEqual(oldName, updatedAccountSecondTime.Name);
}

非常感謝您的回答。 我已經將我的測試方法更新為這兩種方法:

[TestMethod]
public void UpdateAccount_OneValueExpectedToBeUpdated_Updated()
{
    GwIntegrationServiceSoapClient client = new GwIntegrationServiceSoapClient();

    int id = 21; // Target account to update.
    Account accountToUpdate = client.ReadAccount(id);

    string oldName = accountToUpdate.Name;
    string oldEmail = accountToUpdate.Email;
    string newName = Guid.NewGuid().ToString();

    // Update the name only because it's passed by propertiesToUpdate parameter (last one).
    client.UpdateAccount(id, newName, null, null, false, UpdateAccountProperty.Name);

    // Read the record after updating from database
    Account updatedAccountFirstTime = client.ReadAccount(id);

    // The name should be changed
    Assert.AreEqual(newName, updatedAccountFirstTime.Name);
}

[TestMethod]
public void UpdateAccount_NoValueExpectedToBeUpdated_NotUpdated()
{
    GwIntegrationServiceSoapClient client = new GwIntegrationServiceSoapClient();

    int id = 21; // Target account to update.
    Account accountToUpdate = client.ReadAccount(id);

    string oldName = accountToUpdate.Name;
    string oldEmail = accountToUpdate.Email;
    string newName = Guid.NewGuid().ToString();
    string newEmail = Guid.NewGuid().ToString() + "@nomail.com";

    // Update the name only, even a value is passed in newEmail 
    // But in the propertiesToUpdate parameter (last one) only Name value is passed.
    client.UpdateAccount(id, newName, newEmail, null, false, UpdateAccountProperty.Name);

    // Read the record after updating from database
    Account updatedAccountFirstTime = client.ReadAccount(id);

    // The email should not be changed
    Assert.AreNotEqual(newEmail, updatedAccountFirstTime.Email);
}

這很難回答,因為您會在“單元測試”標題下找到的測試類型會有所不同。 大多數情況下,“單元測試”被用作“自動化代碼測試”的同義詞,嚴格來說,它是通過編寫代碼以某種自動方式測試代碼的任何東西。

如果你想寫出好的單元測試,只需要遵循幾條規則:

  • 獨立測試場景。
  • 獨立測試對象。
  • 保持你的測試小而專注。 如果它們膨脹,創建方法來幫助減少樣板代碼並專注於測試的細節。

最終,每次測試測試一件事。 這樣,當一個特定行為或場景發生故障時,您可以立即關注 object 的哪個特定部分發生故障。 在您的示例中,您似乎在一個方法中測試了許多東西,如果第一次調用UpdateAccount成功,但第二次調用失敗,則整個測試失敗並且您不清楚 object 的哪一部分有失敗的。

您應該得到一份The art of unit testing

需要提及的幾點:

  • 每個測試只有一個斷言。
  • 測試方法的命名約定: public void MethodUnderTest_Scenario_Behavior()

看起來不錯。 我的測試中的行數通常較少。

可以將一些樣板移到固定裝置中嗎?

有些人(包括 Roy Osherove 在他的《單元測試的藝術》中)建議每個單元測試使用一個斷言,因為如果第一個斷言失敗,則會拋出異常,並且不會調用任何剩余的代碼,也不會檢查其他斷言。

這可以分成兩個或多個單元測試來實現這一點,例如,一個用於檢查更新后的名稱,一個用於檢查 email 地址,還可能一個用於檢查更改和更改回工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM