本文介绍了难道人们仍然写有TDD测试时所需的代码有很少或几乎没有什么逻辑?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TDD应该有100%的代码覆盖率。这是否意味着一个应该写为属性getter和setter,并且不包含任何真正的逻辑等方法测试,如处理外部API的功能?



实施例1:



下面是一个实例方法(恰好也有在其中所涉及如何最好测试一下,如果我们要进行测试)。这种方法并没有做太多。这是 System.ServiceProcess.ServiceController 停止服务功能的一个门面。目前,该代码没有使用TDD的写入,但如果是,那会是一个应该测试?很少有逻辑在这里。 ,本身并测试不会是有益的。



供参考:如果您想回答就如何最好地测试它(国际奥委会和放大器;适配器模式与迂回)请参阅的。

 公共功能StopService(BYVAL服务名作为字符串,BYVAL timeoutMilliseconds作为双人间)作为布尔器具IWindowsServicesService.StopService 

尝试
昏暗的服务作为新的ServiceController(服务名)
超时暗淡时间跨度为= TimeSpan.FromMilliseconds(timeoutMilliseconds)

服务[停止]()

。如果timeoutMilliseconds< = 0,那么
service.WaitForStatus(ServiceControllerStatus.Stopped)
,否则
service.WaitForStatus(ServiceControllerStatus.Stopped,超时)
端如果

返回service.Status = ServiceControllerStatus.Stopped

抓住EX作为Win32Exception
返回假
抓住EX作为TimeoutException异常
返回false
端尝试

端功能



实施例2:



在情况下,一个主张代码仍具有的一些的逻辑,因此它需要操作的TDD时进行测试,然后关于下面的代码有的逻辑是什么:

 公共功能GetProcess(BYVAL服务名作为字符串)随着工艺
昏暗的ManagementObject作为新的ManagementObject(的String.Format(Win32_service.Name ='{0}',服务名))
昏暗的进程ID为整数= CTYPE(managementObject.GetPropertyValue(的ProcessID) ,整数)
昏暗的过程,过程= process.GetProcessById(进程ID)
返回过程
端功能


解决方案

TDD是的的应该有100%的代码覆盖率。 TDD的往往的有非常高的代码覆盖率,相对于其他的方法。如果你不写一行代码没有一个失败的测试,如果按照严格的,那么,你会得到100%的覆盖率。但我们大多数人没有遵循严格的,完善的代码覆盖的不是TDD 的的目标。高代码覆盖率是一个很好的测试驱动开发的副作用的,但它是不是问题的关键。



我们大多数人不考-drive简单的getter和setter专门为TDD过程的一部分。但我们没有创造这一领域,需要getter和setter,直到我们有必要为它 - 在需要被定义为一个失败的测试。所以吸气&放大器;二传手将被测试作为理所当然的事,因为他们创造需要通过方法我们试驾。



这两个例子你有足够的逻辑,他们是值得试验;其中我试驾两种。


TDD is supposed to have 100% code coverage. Does this mean one is supposed to write tests for property getter and setters, and other methods that contain no real logic, such as dealing with external API functionality?

Example 1:

Below is one example method (which happens to also be the example in this other SO question which deals with how best to test it, if we are going to test it). This method doesn't do much. It's a facade of the System.ServiceProcess.ServiceController functionality of stopping a service. Currently this code was not being written using TDD, but if it was, would it be something that one should test? There is very little logic here. The test in and of itself wouldn't be that helpful.

FYI: If you'd like to answer on how best to test it (IoC & Adapter Pattern vs. Detouring) please see this other SO question.

Public Function StopService(ByVal serviceName As String, ByVal timeoutMilliseconds As Double) As Boolean Implements IWindowsServicesService.StopService

    Try
        Dim service As New ServiceController(serviceName)
        Dim timeout As TimeSpan = TimeSpan.FromMilliseconds(timeoutMilliseconds)

        service.[Stop]()

        If timeoutMilliseconds <= 0 Then
            service.WaitForStatus(ServiceControllerStatus.Stopped)
        Else
            service.WaitForStatus(ServiceControllerStatus.Stopped, timeout)
        End If

        Return service.Status = ServiceControllerStatus.Stopped

    Catch ex As Win32Exception
        Return False
    Catch ex As TimeoutException
        Return False
    End Try

End Function

Example 2:

In case one argues that the code still has some logic, and therefore it needs to be tested when doing TDD, then what about the following code that has no logic:

Public Function GetProcess(ByVal serviceName As String) As Process
    Dim managementObject As New ManagementObject(String.Format("Win32_service.Name='{0}'", serviceName))
    Dim processID As Integer = CType(managementObject.GetPropertyValue("ProcessID"), Integer)
    Dim process As Process = process.GetProcessById(processID)
    Return process
End Function
解决方案

TDD is not supposed to have 100% code coverage. TDD tends to have very high code coverage, as compared to other methodologies. If you don't write a line of code without a failing test, if you follow that strictly, then yes, you'll get to 100% coverage. But most of us don't follow that strictly, and perfect code coverage is not the objective of TDD. High code coverage is a nice side effect of Test-Driven Development, but it is not the point.

Most of us don't test-drive simple getters and setters specifically as part of the TDD process. But we're not creating that field which needs the getter and setter until we have a need for it - where "need" is defined as a failing test. So the getter & setter will be tested as a matter of course, because they're created as needed by methods we are test driving.

Both of your examples have enough logic in them to be worthy of tests; I would test-drive both of them.

这篇关于难道人们仍然写有TDD测试时所需的代码有很少或几乎没有什么逻辑?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:54