TDD, How to write tests to be compiled even if objects do not exist

I am using VS 2012, but this is not very important.

The important thing is that I am trying to do TDD by first writing down all my tests, and then writing the code.

However, the application will not compile because none of my objects or methods exist.

Now, in my opinion, I can create ALL of my tests, but I still run my application so that I can debug, etc. Tests should not interfere with compilation, because objects and methods are missing.

I thought that the whole point is that when developing tests, you can start viewing duplicates, etc., so that you can reorganize before writing one line of code.

So the question is, is there a way to do this, or am I doing it wrong?

EDIT I am using VS2012 and C #

+4
source share
7 answers

Test Driven Development is a very small iteration. You do not define all of your tests in front. You create one test based on one share of one requirement. Then you implement the code to pass this test. Once it passes, you are working on another part of the requirement.

The idea is that trying to get the whole design ahead (whether it's creating detailed class diagrams or creating a lot of tests) means that it will be too expensive for you to change the weakness of your design, t improve your code.

Here is an example. Suppose you decide to use inheritance to connect two objects, but when you started implementing objects, you found that they made them hard. You will find that it would be much easier to test each object individually and link them using the shell instead. What happens is tests that drive your design in a more loosely coupled direction. This is a very good TDD result - you use tests to improve your design.

If you wrote all your tests in advance, considering that your design inheritance solution was a good choice, you would either drop most of the work or you would say "it’s too difficult to make such a change now, so I’ll just live with this non-optimal design."

You can create acceptance tests related to business rules in advance. They are called behavioral tests (part of Behavior Driven Development or BDD), and they are good for testing software features from a user's perspective. But these are NOT unit tests. Unit tests are designed to test code from a developer's perspective. Creating unit tests hits the TDD goal in advance because it will make testing harder, it will prevent you from improving your code and often lead to rebellion and practice failure. Therefore, it is important to do it right.

+3
source

No. This is about coding enough to verify that the required use cases

You can define your tests earlier, but in order to encode test cases, you iteratively write a test if it does not work. Then write a code that ensures that the code passes.

Then rinse and repeat until all your test cases are closed,

Change the comment to the address.

When you create the code, where your programming constructs and errors are identified. Extreme programming gives you the ability to change code without caution, as the test base protects your requirements. Your intentions are good, but the reality is that you will reorganize / remodel test cases as you discover design problems and weaknesses by creating code and a test database.

However, IMHO, in a very general case, a test that does not compile is actually a meta test that cannot be fixed before moving on. This tells you to write code!

+2
source

You should be able to create your own empty class with stub functions, no?

class Whatever { char *foo( const char *name ) {} int can_wibble( Bongo *myBongo ) {} } 

Then you can compile.

+2
source

I see a little problem with

write down all my tests first, and then write the code.

You do not need to write ALL of your tests first, you just need one, make it unsuccessful, make it pass and repeat. This means that ideally you should have the perfect single test at any time.

A compilation failure is considered a failed test in this sense. So, the next step is to do this, for example, add stubs or return the default values ​​to compile them. Then the test will be red, and then process it green.

+2
source

The important thing is that I am trying to do TDD by first writing down all my tests, and then writing the code.

The problem is that “writing all my tests first” most categorically doesn't “do [some] TDD”. Test-based development consists of many small repetitions of the red-green-refactor cycle:

  • Add unit test to the test suite, run it and see how it worked (red)
  • Add enough code to the system under test to run all pass tests (green)
  • Improve the design of the system under test (usually by removing duplication), while maintaining all the tests (refactoring)

If you program the entire huge set of tests, you will always try to get to the green state (all tests).

However, the application will not compile because none of my objects or methods exist.

This is typical of any compiled language; This is not a TDD problem per se. All this means that in order to see how the new test failed, you may have to write a minimal stub for any function that you are currently working on to make the compiler happy.

For example, I can write this test (using NUnit):

 [Test] public void DefaultGreetingIsHelloWorld() { WorldGreeter target = new WorldGreeter(); string expected = "Hello, world!"; string actual = target.Greet(); Assert.AreEqual(expected, actual); } 

And I would have to write this a lot of code to get the application to compile, and the test failed:

 public class WorldGreeter { public string Greet() { return String.Empty; } } 

Once I got the build solution, and I saw one failed test, I can add code to make the first test pass:

  public string Greet() { return "Hello, world!"; } 

Once all the tests have passed, I will be able to review the system under test and see what can be done to improve the design. However, it is important that the TDD discipline go through both the red and green steps before playing with refactoring.

I thought that the whole point is that when developing tests, you can start viewing duplicates, etc., so that you can reorganize before writing one line of code.

Martin Fowler defines refactoring as “a disciplined technique for restructuring existing code, changing its internal structure without changing its external behavior ” (emphasis added). If you haven't written a single line of code, you have nothing to reorganize.

So the question is, is there a way to do this, or am I doing it wrong?

If you want to do TDD, then yes, I'm afraid you are doing it wrong. You can very well deliver excellent code by doing what you do, but this is not TDD; regardless of whether there is a problem for you to decide.

+2
source

Use mock from Wikipedia: mock objects mock objects that control how real objects are managed. A programmer usually creates a mock object to test the behavior of some other object, much like a car designer uses a crash test dummy to simulate a person’s dynamic behavior as a result of a vehicle.

Please refer to this .

0
source

I found this using dynamic for objects that don't exist yet: https://coderwall.com/p/0uzmdw

0
source

Source: https://habr.com/ru/post/1447860/


All Articles