Are TDD tests still ready when the required code has little logic? What for?

TDD must have 100% code coverage. Does this mean that it is assumed that you should write tests for getter and seters properties and other methods that do not contain real logic, for example, concern external API functions?

Example 1:

Below is one example method (which is also an example in this other SO question , which discusses how best to test it if we are going to test It). This method does little. This is the facade of the System.ServiceProcess.ServiceController service stop function. This code has not been written using TDD at this time, but if that were the case, would it be something to check? There is very little logic. The test itself would not be useful.

FYI:. If you want to answer the question of how best to test it (IoC and Adapter Pattern vs. Detouring), 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 someone claims that the code still has some logic, and therefore it needs to be tested when executing TDD, then what about the following code with 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 
+3
c # unit-testing tdd code-coverage
source share
3 answers

TDD should not cover 100% of the code. TDD tends to have very high code coverage compared to other methodologies. If you do not write a line of code without a failure test, if you follow this strictly, then yes, you will get 100% coverage. But most of us do not follow this strictly, and perfect code coverage is not the purpose of TDD. High code coverage is a good side effect of test-based development, but that's not the point.

Most of us do not test simple getters and setters specifically as part of the TDD process. But we do not create this field, which requires getter and setter, until we need it, where "necessity" is defined as a failed test. Thus, the getter and setter will be tested for granted, because they are created as necessary by the methods we test.

Both of your examples have enough logic in them to be worthy of tests; I would check them both out.

+11
source share

In the interest of full disclosure: I am not an expert in TDD (I actually do not use it and never have), but I think I can argue out of my ignorance for TDD lawyers in this case.

Imagine a situation where you have a simple / zero logic function, for example, Example 2. Suppose you have implemented your own version of GetProcessById , which functions very slightly differently, but interchangeably with the Process object. You decided not to write a test for this function and say: "Ah, I just delegate it to a trusted library, I can not ruin it." There is your first mistake. β€œI can't mess it up” is the worst lie programmers ever regularly tell themselves.

Suppose that after six months you realize that you need to expand the Process with some additional materials. You delegate all the correct methods and override GetProcessById . Now you officially need to test this "zero logic" method. And this is a disaster. Polymorphism and many other features of programming languages ​​(even those that are not strictly object-oriented) lead to code that does not do what you imagine.

So, if you adhere to a strict TDD methodology, and you strive for 100% testing coverage, you will need to test the methods of "zero logic", as well as those that are in two versions.

The only exception that I can imagine is specific to .NET, and these are automatically implemented properties when it makes no sense to test that Getter and Setter are working correctly, because even if they didn’t do anything you could do it. Check the object wrapped in the property, not its property.

TDD people, does this describe him very well?

+5
source share

Full disclosure: I do not use formal TDD and never

In your code examples, a third-party complex API has been encapsulated to do something. Where do the errors come from?

Most errors will be caused by unexpected, possibly undocumented, behavior from the API. It is very difficult to get these things to work in any situation (Windows version / regional setting / network setting, etc.). You can truly become a world famous blogger by simply documenting the mistakes and misunderstandings that people have made using the Win32 API.

  • The most important tests are tests against the real API. To create processes or services, a setup and break code is required.
  • Mocking / IoC is not very valuable. You simply prove that you make certain API calls in a specific order. You need to prove that your code achieves what you want in all situations that it will face in the real world.
  • I really think you cannot do test development against ridicule. Imagine that you found a bug in a third-party API ( this happens ). You will need to get around the error. You may even have to completely change your approach, perhaps PInvoke to the Win32 API or some COM object. In this situation, the mockery of the tests will be just a waste of time.
    • First you need to compile a workaround code that actually works against the real API.
    • Then you will need to edit the ridiculed tests according to a workaround.
    • At first you couldn't edit the ridiculed tests, because you need to try a workaround to see if it really works.

My world is a desktop software for the vertical market. We have a library of routines like yours, and we have realistic tests for them. Sometimes we find an error in a specific version of Windows / regional settings / independently. Then we expand the tests to reproduce the error and verify the fix, and be sure to run them in our test environment. Note to the reader: if your code should run on only one server , your world may be different and you won’t need to do this.

+1
source share

All Articles