標籤:
單元測試
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
以下關於單元測試的總結,是基於目前工作的內容進行的匯總,包括了單元測試的定義,單元測試assertion語句,單元測試的架構以及實踐中的注意事項等。其中【***】為解釋說明。在此推薦幾本有關單元測試的書籍供參考。《單元測試的藝術》《單元測試之道junit(Java版)》《單元測試之道Nunit(C#版)》。
Overview
一個UT當中,包括了準備資料,釋放資源,執行要驗證的那段邏輯代碼,以及結果的驗證等。
準備資料:包括setUpTestCase 和setUp【setUpTestCase different from setup, setUpTestCase is Sets up test environment for test case, it should be run only one time, setup should be run first when run every test case.】setUpTestCase的資料準備是對一個UT中的所有test case所做的資料準備,這個在運行UT時系統僅運行一次,而對於setUp,每跑一個test case,都會跑一次setUp。
釋放資源:包括tearDownTestCase和tearDown【When finished test case, release source, tearDownTestCase is deferent from teardown, one for all test case, one for every test case.】,一個是對整個UT運行結束後釋放資源,一個是每跑完一個test case,立即釋放資源,這樣做的好處是防止了資料不乾淨產生的結果驗證不正確的困擾。
執行要驗證的那段邏輯代碼:則我們需要寫單元測試的部分是希望在系統薄弱的地方找出更多的bug,所以,開發的代碼中沒有邏輯的代碼,我們不需要花時間去驗證。應該把時間和精力放在可能出現問題的地方。
結果的驗證:在準備資料的時候,我們其實已經知道了代碼運行之後的結果是什麼,我們只需要知道它是否跟我們的期望是一致的,跟產品的功能是一致的。還有不要為了測試而測試,case跑通了不重要,重要的是你是不是測到點上了。
準備不同的資料去儘可能的驗證所有的分支,保證測試的覆蓋率。
綜上所述,一個UT的執行順序是:setUpTestCase ----> setup----> testCase1---->teardown----> setup----> testCase2---->teardown ...----> tearDownTestCase .
基礎知識鋪墊:
一、什麼是UT:
UT 指單元測試,是為了證明某段程式碼為確實和開發所期望的是一致的,驗證代碼的功能及操作特性,換句話說就是用一段代碼驗證另一端代碼的邏輯正確與否。
二、UT中的Assertion語句:
①驗證實際值和期望值是否一致:
assertEquals(expected, actual, string message);
asserNotEquals(expected, actual, string message);
②驗證一個給定的object是否為空白或者非空:
assertNull(Object object, string message);
assertNotNull(Object object, string message);
③驗證期望值和實際值所引用的對象是否為相同的對象:
assertSame(expected, actual, string message);
assertNotSame(expected, actual, string message);
④驗證在條件下是否為ture或false:
assertTure(anytype condition, string message);
assertFalse(anytype condition, string message);
⑤驗證期望的object和實際的object是否為相等的。
assertObjectEquals(expected, actual, string message);
assertNotObjectEquals(expected, actual, string message);
⑥Checks whether two values are equal with regard to a delta.
assertRealEquals(expected, actual, delta, string message);
⑦Checks whether two UTC date time values are equal with regard to a delta.
assertUTCDateTimeEquals(expected, actual, string message, delta)
三、Attribute we used
Use [SysTestGranularityAttribute(SysTestGranularity::Component)]? ? attribute to mark intentional component tests.
Use [SysTestGranularityAttribute(SysTestGranularity::Integration)]? ? attribute to mark intentional Integration tests.
Use [SysTestGranularityAttribute(SysTestGranularity::BusinessCycle)]? ? attribute to mark intentional Business Cycle tests.
[SysTestInactiveTest] attribute to ignore the case.
[SysTestMethod] attribute for each test, it can be make case can be run.
[SysTestCheckInTest] attribute for each test or test class.
In SetupTestCase for all tests in the class, add attribute to the class :
#isoCountryRegionCodes
SysTestCaseCountryRegionDependency(#isoTH)] to set country region code for test case.
[SysTestTarget(classstr(CustVendTransData), UtilElementType::Class) attribute to specify the target element for code coverage.
[SysProductAreaAttribute("***")] attribute to define the product area the class belongs to.
四、Framework of UT
[attribute]
class ClassName extends class
{
Declare variable;【Use const to declare variable.】
/// <summary>
/// Sets up test environment for test case.
/// </summary>
Public void setUpTestCase()
{
ttsbegin; 【Begin transaction, and commit transaction or abort transaction, to prevent data is not clean】
Prepare Data;
ttscommit;
}
public void tearDownTestCase()
{
infolog.clear(0);
testData.tearDown();
testCompany.tearDown();
super();
}
/// <summary>
/// Sets up test.
/// </summary>
public void setUp()
{
}
public void tearDown()
{
super();
}
/// <summary>
/// Comments.
/// </summary>
[SysTestMethod]
public void testcase1() 【testcase1 should be use a meaningful name, and lower first letter.】
{
// Arrange
[Setup the test case]
// Act
[Act on the system under test/unit]
// Assert
[Validate the result]
}
/// <summary>
/// Comments. 【Xml document should be add to every test method, capital letters and summary should be a meaningful sentence.】
/// </summary>
[SysTestMethod]
public void testcase2()
{
// Arrange
[Setup the test case]
// Act
[Act on the system under test/unit]
// Assert
[Validate the result]
}
}
五、Tips of UT
- Tests should include three comments separating sections of the code:
// Arrange
[Setup the test case]
// Act
[Act on the system under test/unit]
// Assert
[Validate the result]
2.Add ttsbegin/ttscommit/ttsabort 來進行交易處理,保證資料庫不包括不完整的操作結果。
Ttsbegin-------開始交易處理,
Ttscommit----提交交易處理,
Ttsabort-------復原交易處理
只有全部語句都成功執行後,交易處理才算成功;
若其中有一個語句執行失敗,則整個處理就算失敗,
並恢複到處理前的狀態。
3.Do ensure a test has at least one assertion or expected exception.
4.Use data provider if you use existing data in demo data.
5.在準備資料時,關於插入的資料是自己insert進去的,至於把什麼值插入到哪張表裡面是根據class中的分支,找到的依據,以及表和表的關係,如果有query,在query中找到的依據,進行總結,總結出共有多少種測試點,儘可能的覆蓋所有的分支情況,從而將測試點寫成代碼UT,從而驗證代碼的邏輯是否符合功能需求。
博主:海寧
聯絡:[email protected]
我的單元測試之總結