如何使用用例进行单元测试

时间:2015-03-19 23:46:09

标签: c# tdd

这是我的用例 enter image description here

要开始在TDD中编写单元测试,我需要提出要测试的类和方法,例如: ClassUnderTest.MethodUnderTest()

名词有:CandidateAdminUserAccountCredentialsPasswordResumeWidget(系统),最后但并非最不重要的是SomeOtherAbstractionIDidNotThinkOf(例如SecurityService)。

动词包括:Login()Logout()Register()CreateAccount()RecoverPassword()Unlock()ResetPassword(),{ {1}},最后但并非最不重要的是Lockout()

为了使这个问题最简单,让我们看看我们是否可以坚持使用SomeOtherActionIDidNotThinkOf()。让我看看我是否可以为该方法启动单元测试。

你会在某处使用Login方法吗?

如果是这样,你会把它放在哪个班级?为什么呢?

如果没有,你会使用什么类和方法?为什么?

1 个答案:

答案 0 :(得分:0)

我给你一个关于如何启动TDD的小提示。首先,考虑如何根据您的用例设计您的软件。(例如:帐户类可以包含登录,注销等方法)

[TestFixture]
public class AccountTest
{
   public Account underTest;

   [SetUp]
   public void setUp()
   {
     underTest = new Account();
   }

   [Test]
   public void TestLoginWithValidCredentials()
   { 
      bool isValid = underTest.Login('userName', 'password');         
      Assert.True(isValid );
   }

   [Test]
   public void TestLoginWithInValidCredentials()
   {
       bool isValid = underTest.Login('', '');         
       Assert.False(isValid );
   }

   [Test]
   //Assert Expected Lockout exception here
   public void TestLockoutByPassingInValidCredentialsMoreThan5Times()
   {

       for (int i =0; i< 5; i++)
       {     
          bool isValid = underTest.Login('', '');    
          Assert.False(isValid );     
       }           
        // the system should throw an exception about lockout
        underTest.Login('', ''); 
   }      

   [Test]
   public void TestCreateAccount()
   {
      //Register event can directly call this method
      bool isCreated = underTest.Create(/*User Object*/);
      Assert.True(isCreated );
   }

   [Test]
   public void TestLogout()
   {
      bool success = underTest.Logout('userName');          
      Assert.True(success);          
   }

   [Test]
   public void TestReset()
   {
      // both Unlock and RecoverPassword event can share this method
      bool success = underTest.Reset('userName');          
       // mock the password reset system and return the expected value like 'BSy6485TY'
      Assert.AreEqual('BSy6485TY', password);
   }       
}

更新:添加了更多测试用例以帮助您设计系统。

相关问题