🔷 C#

Pruebas de código en C# (.NET): guía detallada

Here is the translated HTML article in English, following all your rules. ```html

What is it and why? (a real-life analogy)

Imagine you are baking a pie using a new recipe. You mix flour, eggs, sugar, and put it in the oven. But before you serve this pie to guests, you definitely taste it: is it too salty? Is it baked inside? Is it burnt on the bottom? This process of "tasting" is testing.

In programming, it's exactly the same. When you write code, you create a "recipe" for the computer. But the computer is a very literal "cook." If the recipe says "add salt" and you forgot to specify the amount, the computer might add the whole pack. Or if you wrote "stir 10 times" but needed to "beat with a mixer," the pie won't turn out.

Code testing is a way to check that your program works exactly as you intended and doesn't "break" in unexpected situations. It's like insurance: you can be sure that after changing one part of the program, another part won't stop working.

"Code without tests is not a program, but just a set of symbols that, perhaps, once worked." — programmer's folk wisdom.

Why does a school student or a beginner need this? Imagine you wrote a "Guess the Number" game. You ran it, played it — everything works. But a week later you decided to add a score counter. You changed the code, ran it — the game works, but now the number is guessed from 1 to 50, not from 1 to 100. You accidentally changed a line. If you had tests, they would immediately say: "Error! The game should guess numbers up to 100, but it guesses up to 50!"

Testing saves hours, days, and nerves. Professional programmers spend up to 40% of their time on tests. And this is not because they don't know how to write code, but because they want to sleep peacefully, knowing that their program won't crash at the most crucial moment.

Theory: how it works from the inside

In the .NET (C#) world, testing is divided into several levels. Imagine a nesting doll:

  • Unit Testing — checking the smallest part of a program: one method, one function. Like checking one egg for freshness before cracking it into the batter.
  • Integration Testing — checking how different parts of the program work together. Like checking that flour and eggs actually mix into a batter and don't scatter across the table.
  • Functional Testing (E2E) — checking the entire program as the user sees it. Like tasting the finished pie.

In this article, we will focus on unit testing, because it is the foundation. It is written by the developers themselves and runs automatically in fractions of a second.

How does it work from the inside?

You have a Calculator class with a method Add(int a, int b) that returns the sum. You write a test:

  1. Create an instance of the Calculator class.
  2. Call the Add(2, 3) method.
  3. Check: is the result equal to 5?

If the result is not 5 — the test fails (red). If it is 5 — the test passes (green). Simple.

In C#, there is a special framework for this — xUnit (or NUnit, MSTest). This is a library that gives you tools: attributes for marking tests, classes for checking results (Assert).

The process itself looks like this:

  1. You create a separate project in the Solution — for example, MyApp.Tests.
  2. Add a reference to the main project (MyApp).
  3. Write classes with tests.
  4. Run the tests through a special window in Visual Studio or via the console with the command dotnet test.
Important: tests are also code. They need to be written as carefully as the main code. Dirty tests are worse than no tests because they create a false sense of security.

Syntax in C# (.NET): all forms and variants

In C#, there are three main testing frameworks: MSTest (from Microsoft), NUnit (old but popular), and xUnit (modern standard). We will use xUnit, as it is recommended by Microsoft for new projects.

Main attributes (markers) in xUnit

Attribute Purpose Example
[Fact] Marks a method as a test without parameters. The simplest test. [Fact] public void Add_TwoNumbers_ReturnsSum()
[Theory] Marks a method as a test with parameters. Allows running one test with different data. [Theory] [InlineData(1,2,3)]
[InlineData] Provides data for [Theory]. Can be used multiple times. [InlineData(1, 2, 3)] [InlineData(0, 0, 0)]
[Trait] Allows categorizing tests (e.g., "Fast", "Slow"). [Trait("Category", "Unit")]

Main assertion methods (Assert)

Method What it checks Example
Assert.Equal(expected, actual) Equality of two values Assert.Equal(5, result);
Assert.NotEqual(expe Inequality of two values Assert.NotEqual(4, result);
```