📜 ⬆️ ⬇️

Introduction to unit testing for c # projects in MonoDevelop

Unit tests are used in software development. They can be created both after writing the source code, and before that, it all depends on your preferences and religion, or the preferences of your company. Development through testing (TDD) is a rather controversial impression. Someone thinks that this is a rather useless thing, but is inclined to disagree. Useless TDD can not be called exactly. Creating a test covering the intended change in the program, and then writing code that would allow to pass this test, significantly simplifies development. Unit tests are also used to check the already created functionality. However, to achieve 100% coverage of the program code by unit tests is almost impossible.

Plan:
  1. Creating a program HelloWorld.
  2. Writing unit tests for HelloWorld.
  3. Writing source code for passing unit tests.


Why was C # chosen? This is a fairly easy to learn programming language. In which you do not need to think about the allocation of memory and its cleaning. A wide selection of different .NET libraries makes it easy to accomplish complex tasks. In addition, programs compiled in one platform using CLR can be run on another platform, in which there is a CLR implementation.

Creating a HelloWorld program


  1. Launch MonoDevelop and create a new HelloWorld project.
  2. Create a new class Goodbyer.
  3. In addition to the constructor without parameters, create a constructor with a string as a parameter. As well as a method that returns a string and allows you to set the value of the _who field.

Creating unit tests


  1. Create a new NUnit test library project in this solution.
  2. Add a link to the HelloWorld project.
  3. Create several unit tests for the constructor with parameters and for the method that specifies the value of the _who field. Picture 5
  4. The following image shows that not all tests were passed. Since the program does not throw an exception when we set the _who field to empty.

Changing the class Goodbyer for passing unit tests


  1. Change the Goodbyer class so that it throws an exception when an empty string is written in the _who field.
  2. The above image shows that all the tests have been passed.

')
That's all. If you have done all the steps, then you can be congratulated on mastering the simplest principles of unit testing.

Links


Using NUnit with MonoDevelop: www.dijksterhuis.org/using-nunit-with-monodevelop - Tutorial on creating unit tests in NUnit with MonoDevelop.

C # Unit Test Tutorial: www.rhyous.com/programming-development/csharp-unit-test-tutorial - Tutorial on unit testing.

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


All Articles