I'm CaLendarW Blog

Tag: test-driven

Timer for Performance Testing

by calendarw on May.13, 2009, under coding snippet, testing

呢排重溫緊 Pragmatic Unit Testing in C# with Nunit, 開始試緊寫有關 Performance 既 Test Case, 書中有一段有關 Performance 既 Code:

[Test]
public void FilterRanges()
{
    Timer timer = new Timer();
    String naughty_url = "http://www.xxxxxxxxxxx.com";
    // First, check a bad URL against a small list
    URLFilter filter = new URLFilter(small_list);
    timer.Start();
    filter.Check(naughty_url);
    timer.End();
    Assert.IsTrue(timer.ElapsedTime < 1.0);
    // Next, check a bad URL against a big list
    filter = new URLFilter(big_list);
    timer.Start();
    filter.Check(naughty_url);
    timer.End();
    Assert.IsTrue(timer.ElapsedTime < 2.0);
    // Finally, check a bad URL against a huge list
    filter = new URLFilter(huge_list);
    timer.Start();
    filter.Check(naughty_url);
    timer.End();
    Assert.IsTrue(timer.ElapsedTime < 3.0);
}

段 code 係一個幾好既 example 去講點 Test Performance, 但當真係要試果陣, 就發現左樣野, 就係我搵唔到 Code 中既 Timer Class, 係我在 System 入面既幾個 namespace 中, 都搵唔到啱用既 Timer, 因為 System namespace 入面既 Timer 大部份都係用黎 Trigger Timeout Event, 而當中既 Stop method 都只係用黎停止 Event Trigger, 而沒有任何計時結果做到出黎, 所以經過一輪網上既搜尋之後, 得出以下 Timer, 主要目的係用黎計時, 仔細度高, 最啱用黎作 Performance Testing 之用!!~

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace TestUtil
{
    public class HighResolutionTimer
    {
        private long frequency;
        private long start;
        private long stop;

        public HighResolutionTimer()
        {
            QueryPerformanceFrequency(ref frequency);
        }

        public float ElapsedTime
        {
            get
            {
                float elapsed = (((float)(stop - start)) / ((float)frequency));
                return elapsed;
            }
        }

        public void Start()
        {
            QueryPerformanceCounter(ref start);
        }

        public void Stop()
        {
            QueryPerformanceCounter(ref stop);
        }

        [System.Runtime.InteropServices.DllImport("KERNEL32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern bool QueryPerformanceCounter([In, Out] ref long performanceCount);

        [System.Runtime.InteropServices.DllImport("KERNEL32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern bool QueryPerformanceFrequency([In, Out] ref long frequency);

    }
}

HighResolutionTimer 來源 : 在 C# 中實現高性能計時

Leave a Comment :, , more...

Acceptance Test Driven Development

by calendarw on Apr.30, 2009, under requirement, testing

Last night, I went to join the meeting of Agile Hong Kong, the topic is Acceptance Test-Driven Development(ATDD), presented by Steven Mark, which look-like great, user friendly, and example-driven concept to improve the software quality and meet the customer expectation, the different between TDD and ATDD is that the TDD is more focus on unit testing, test case written by developer and ATDD is focus on the customer example, test case written by user in readable format, one of practice is customer/business analyst provide html “table” of example using wiki and let the FIT or Robot framework read the html “table” and provide test result.

ATDD is great, but currently I still need a time to pickup the current unit testing with NUnit, lack of experience and no partnership in agile development is my weakness, and I need more time to train up my experience. I hope that I have opportunity to apply agile in future although current environment seen doesn’t permit.

Another good thing in this meeting is meeting with Rex, who is the Ruby developer and IT company owner, we have similar idea of Hong Kong IT Event Calendar before and he did better than me. Also, in this meeting, free pizza was good, and small gift (should be the score card) was offered.

Leave a Comment :, more...

Why Automate Test?

by calendarw on Apr.15, 2009, under design, diary, testing

Test Driven Development, 係以測試來驅動程式既設計, 目的係為了提高軟件既質素.

好多人覺得測試對軟件方面只在於用戶接受測試 (UAT), 因為只要通過 UAT, 公司就可以袋袋平安. 不過, UAT 通常都浪費人手, 而且該次 UAT 只計對於該次既軟件需求, 當需求需要更改, 所有既測試都要人手重頭做過, 浪費人力物力.

自動化軟件測試既好處係可以減省人手及時間, 以及提高軟件既質素及完整度.

以下係一個由 NUnit 提供既自動化測試例子:

// 一個 object oriented 既 Account class
namespace bank
{
  public class Account
  {
    private float balance;
    public void Deposit(float amount)
    {
      balance+=amount;
    }

    public void Withdraw(float amount)
    {
      balance-=amount;
    }

    public void TransferFunds(Account destination, float amount)
    {
    }

    public float Balance
    {
      get{ return balance;}
    }
  }
}
namespace bank
{
  using NUnit.Framework;

  [TestFixture]
  public class AccountTest
  {
    // 自動化測試來測試 TransferFunds Method
    [Test]
    public void TransferFunds()
    {
      Account source = new Account();
      source.Deposit(200.00F);
      Account destination = new Account();
      destination.Deposit(150.00F);

      source.TransferFunds(destination, 100.00F);
      Assert.AreEqual(250.00F, destination.Balance);
      Assert.AreEqual(100.00F, source.Balance);
    }
  }
}

以上只係其中一個例子用來測試軟件既完成度, 在現實上, Test Case 除左要測驗一般情況外, 仲要測試錯誤情況出來既結果, 保証軟件質素, 所以正常使用既情況係唔會得一個 Test Case 咁少.

要實行 Test Driven Development, 通常要有一個良好既 Object Oriented Design 以及 Design for Test, 如果沒有這兩個設計既技巧, 那測試既質素有可能會受影響.

愈多 Test Case 未必代表軟件的準確率愈高, 正常情況 Test Case 既數量應該多過 Use Case 既數量, 而且會因應不同既考慮點而增加一定的測試數量, 以我所知, 在某些大學既科目中, 有計算要有多少 Test Case 既試題 (因為我都有睇過), 用作驗證是否足夠全面地測試每一行代碼.

在測試失敗時, 有問題既地方除了可能係軟件本身外, 自動化測試部份亦有可能會有錯誤, 錯誤既多少在乎實現 Test Case 既人對軟件測試既知識同經驗所得.

雖然軟件在有測試既環境下推出, 但有時都有可能會在軟件發現有 bug 既情況, 當遇到這個情況下, Programmer / Tester 應該用 Test Case 去模擬問題既環境, 當得出相同既結果後才可以更改軟件, 因為未能模擬出相同環境下更改軟件, 失敗後亦未知是軟件問題定 Test Case 問題, 所以 Programmer 應該先更改 Test Case 後才更改軟件, 而更改軟件途中, Programmer 亦可透過測試來避免 side effect 出現, 所以不段發現問題環境才能提高軟件既完整性, 以及提高軟件人員對軟件既信心.

測試, 除了要滿足軟件既完整性, 亦要滿足不段改變既需求, 世界變, 軟件同測試不變就只會不進則退, 所以為新既需求而更改軟件及擴充其功能既環境下, 自動化測試係必須的.

一年前我工作既地方, 因為無自動化測試, 軟件無 Design for Test, 要用到硬件 及 3rd Party 提供既模擬程式下, 人手測試既時間應該超過一個月, 除了測試一個月既時間外, 測試既人手通常都要兩至三人, 而且大部份都係重複既輸入工作, 以及已改幾句 code 而重做半小時既測試結果既慘況下, 自動化測試變成我寫軟件生活既其中一個有興趣深入研究既地方.

Leave a Comment :, , , , more...

Code Jam: Automated Unit tests with Mocks and Stubs

by calendarw on Oct.09, 2008, under diary

This is my second times go agile Hong Kong event, I go because I want to increase my testing knowledge.

This code jam introduced the mock frameworks (like Mockito, EasyMock and JMock) and discussed the limitation and usability, also demonstrated different implementation of Java Mock frameworks, it is interesting but how can I make it work with me? DI is another part of discussion, we know that DI is really powerful and very flexible for software, so some of point are discussed in this event.

I program with NUnit sometime but I have not enough knowledge to make the tests well, lots of my work had focused on domain design, reusable framework design and UI flow implementation. I have a lots of new thing are needed to be study in new company, and new environment, but I haven’t time to do so.

I hope that I can find somewhere to make the mock framework more useful for me to make the quality software product.

1 Comment :, more...

Today finding

by calendarw on Aug.13, 2008, under daily finding

2008 Best of Open Source Software Awards
It’s just for reference.

10 Ultimate Rules for Effective System Administration
Interesting, but no time to do it, always for Hong Kong!!

10 quick fixes for the worst security nightmares
Admin Reference~~

Is unit testing doomed?
Supporting Document of TDD. Benefit and cost.

.NET 3.5 Enhancements Training Kit RTM
又出新野……

Can telecommuting kill your career?
SOHO idea.

Leave a Comment :, more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!