Skip to main content
added 180 characters in body
Source Link
Doc Brown
  • 220.3k
  • 35
  • 410
  • 623

First, I am convinced it is not a very effective approach trying to test a function which was not written in a testable manner in conjunction with some "no change to the source code" policy. Often, some changes to the source code take only a small effort and have a very low risk of breaking somethinganything, but they reducewhilst reducing the effort of writing unit tests by an order of magnitude.

However, depending on the actual programming language, it may be possible to embed these functions into a testing environment where the variables system or database can be initialized by "test objects" or "mocks" from outside. That can make it possible to test the functions of your example without changing anything to thein source code of thosefile where the functions you want to test "lives".

For example, in C or C++, you might be able to utilize the preprocessor for this, or include files which are different in the test environment from the development enviroment. In Java or C#, you may be able to set up a newspecial test project, containing a "mock database"mock "database" and a "mock system"mock "system" object for this, and reference the "function under test" into this environment.

However, this is much easier if you change the code slightly in a way you can inject the "system" or the function system.day directly by the caller. Michael Feathers called this creating a seam in your code - a place where you can alter behavior without editing the code in place.

First, I am convinced it is not a very effective approach trying to test a function which was not written in a testable manner in conjunction with some "no change to the source code" policy. Often, some changes to the source code take only a small effort and have a very low risk of breaking something, but they reduce the effort of writing unit tests by an order of magnitude.

However, depending on the actual programming language, it may be possible to embed these functions into a testing environment where the variables system or database can be initialized by "test objects" or "mocks" from outside. That can make it possible to test the functions of your example without changing anything to the source code of those functions you want to test.

For example, in C or C++, you might be able to utilize the preprocessor for this, or include files which are different in the test environment from the development enviroment. In Java or C#, you may be able to set up a new project, containing a "mock database" and a "mock system" for this, and reference the "function under test" into this environment.

First, I am convinced it is not a very effective approach trying to test a function which was not written in a testable manner in conjunction with some "no change to the source code" policy. Often, some changes to the source code take only a small effort and have a very low risk of breaking anything, whilst reducing the effort of writing unit tests by an order of magnitude.

However, depending on the actual programming language, it may be possible to embed these functions into a testing environment where the variables system or database can be initialized by "test objects" or "mocks" from outside. That can make it possible to test the functions of your example without changing anything in source code file where the functions you want to test "lives".

For example, in C or C++, you might be able to utilize the preprocessor for this, or include files which are different in the test environment from the development enviroment. In Java or C#, you may be able to set up a special test project, containing a mock "database" and a mock "system" object for this, and reference the "function under test" into this environment.

However, this is much easier if you change the code slightly in a way you can inject the "system" or the function system.day directly by the caller. Michael Feathers called this creating a seam in your code - a place where you can alter behavior without editing the code in place.

added 357 characters in body
Source Link
Doc Brown
  • 220.3k
  • 35
  • 410
  • 623

First, I am convinced it is not a very effective approach trying to test a function which was not written in a testable manner in conjunction with some "no change to the source code" policy. Often, some changes to the source code take only a small effort and have a very low risk of breaking something, but they reduce the effort of writing unit tests by an order of magnitude.

However, depending on the actual programming language, it may be possible to embed these functions into a testing environment where the variables system or database can be initialized by "test objects" or "mocks" from outside. That can make it possible to test the functions of your example without changing anything to the source code of those functions you want to test.

For example, in C or C++, you might be able to utilize the preprocessor for this, or include files which are different in the test environment from the development enviroment. In Java or C#, you may be able to set up a new project, containing a "mock database" and a "mock system" for this, and reference the "function under test" into this environment.

First, I am convinced it is not a very effective approach trying to test a function which was not written in a testable manner in conjunction with some "no change to the source code" policy. Often, some changes to the source code take only a small effort and have a very low risk of breaking something, but they reduce the effort of writing unit tests by an order of magnitude.

However, depending on the actual programming language, it may be possible to embed these functions into a testing environment where the variables system or database can be initialized by "test objects" or "mocks" from outside. That can make it possible to test the functions of your example without changing anything to the source code of those functions you want to test.

First, I am convinced it is not a very effective approach trying to test a function which was not written in a testable manner in conjunction with some "no change to the source code" policy. Often, some changes to the source code take only a small effort and have a very low risk of breaking something, but they reduce the effort of writing unit tests by an order of magnitude.

However, depending on the actual programming language, it may be possible to embed these functions into a testing environment where the variables system or database can be initialized by "test objects" or "mocks" from outside. That can make it possible to test the functions of your example without changing anything to the source code of those functions you want to test.

For example, in C or C++, you might be able to utilize the preprocessor for this, or include files which are different in the test environment from the development enviroment. In Java or C#, you may be able to set up a new project, containing a "mock database" and a "mock system" for this, and reference the "function under test" into this environment.

Source Link
Doc Brown
  • 220.3k
  • 35
  • 410
  • 623

First, I am convinced it is not a very effective approach trying to test a function which was not written in a testable manner in conjunction with some "no change to the source code" policy. Often, some changes to the source code take only a small effort and have a very low risk of breaking something, but they reduce the effort of writing unit tests by an order of magnitude.

However, depending on the actual programming language, it may be possible to embed these functions into a testing environment where the variables system or database can be initialized by "test objects" or "mocks" from outside. That can make it possible to test the functions of your example without changing anything to the source code of those functions you want to test.