Martin Fowler的《重构》这本书基本上每个程序员都会看,对于做单元测试的测试工程师来说,测试的代码本身也是程序,也需要重构。最近在看《XUnit Test Patterns》,把以前的做的东西重新梳理了一下,并且落实到新的项目中。
首先来看看一个最原始的单元测试代码:
[TestMethod] public void GetPaymentAccountByOwnerID() { int ownerId = 1300100000; //Delete the account TestHelper.DeletePaymentAccountByOwnerId(ownerId); //Here should be create an account PaymentAccount paymentAccount = PaymentGateway.PaymentProvider.GetPaymentAccountByOwnerID(ownerId, AccountOwnerType.NormalUser); //Verify the payment account instance Assert.IsTrue(paymentAccount.AccountID > 0); Assert.AreEqual(DateTime.Now.DayOfYear, paymentAccount.CreateTime.DayOfYear); Assert.AreEqual(DateTime.Now.DayOfYear, paymentAccount.UpdateTime.DayOfYear); Assert.AreEqual(0, paymentAccount.Balance, 0.0001); Assert.AreEqual(0, paymentAccount.AvailableBalance, 0.0001); Assert.AreEqual(0, paymentAccount.FreezeAccount, 0.0001); } |
以上是个很简单的单元测试代码,应用了AAA原则,首先做好准备(删掉原有的数据),然后执行测试,最后再验证返回结果。看起来很好也很清晰。首先发现一个问题,就是那一堆Assert让人觉得迷惑,究竟想干啥?其实那6句Assert都是验证程序返回的PaymentAccount对象是是否符合设计。我把这些Assert语句提取出来,作为一个方法,让测试代码更加容易让人明白。也就有了版本2。
[TestMethod] public void GetPaymentAccountByOwnerID() { int ownerId = 1300100000; //Delete the account TestHelper.DeletePaymentAccountByOwnerId(ownerId); //Here should be create an account PaymentAccount paymentAccount = PaymentGateway.PaymentProvider.GetPaymentAccountByOwnerID(ownerId, AccountOwnerType.NormalUser); //Verify the payment account instance PaymentAccountAssertion(paymentAccount); } private void PaymentAccountAssertion(PaymentAccount paymentAccount) { Assert.IsTrue(paymentAccount.AccountID > 0); Assert.AreEqual(DateTime.Now.DayOfYear, paymentAccount.CreateTime.DayOfYear); Assert.AreEqual(DateTime.Now.DayOfYear, paymentAccount.UpdateTime.DayOfYear); Assert.AreEqual(0, paymentAccount.Balance, 0.0001); Assert.AreEqual(0, paymentAccount.AvailableBalance, 0.0001); Assert.AreEqual(0, paymentAccount.FreezeAccount, 0.0001); } |
以上代码看起来就舒服多了,比较清晰、简短。但是有一个问题,就是后面的3条Assert语句的期望结果是Hard code的,这样很不利于这个自定义的Assert方法的重用。改!我会把那3个〇抽取成这个方法的一个参数,这个自定义的Assert方法变得更加灵活了。代码不贴上来了,因为有一个问题,就是如果我们还需要对PaymentAccount增加一些属性,而这些属性的值都需要被验证,那么这个 PaymentAccountAssertion方法的参数就会越来越长。超长的方法参数也是一个不好的味道,我改!改为传递一个期望的 PaymentAccount对象进来,这样就是两个参数了,很好。
[TestMethod] public void GetPaymentAccountByOwnerID() { int ownerId = 1300100000; //Delete the account TestHelper.DeletePaymentAccountByOwnerId(ownerId); //Here should be create an account PaymentAccount paymentAccount = PaymentGateway.PaymentProvider.GetPaymentAccountByOwnerID(ownerId, AccountOwnerType.NormalUser); //Verify the payment account instance PaymentAccount expected = new PaymentAccount(); PaymentAccountAssertion(expected, paymentAccount); } private void PaymentAccountAssertion(PaymentAccount expectedObject, PaymentAccount paymentAccount) { Assert.IsTrue(paymentAccount.AccountID > 0); Assert.AreEqual(DateTime.Now.DayOfYear, paymentAccount.CreateTime.DayOfYear); Assert.AreEqual(DateTime.Now.DayOfYear, paymentAccount.UpdateTime.DayOfYear); Assert.AreEqual(expectedObject.Balance, paymentAccount.Balance, 0.0001); Assert.AreEqual(expectedObject.AvailableBalance, paymentAccount.AvailableBalance, 0.0001); Assert.AreEqual(expectedObject.FreezeAccount, paymentAccount.FreezeAccount, 0.0001); } |