Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
How to unit test a console program that reads input and writes to the console

Let's say I have a simple program like

using System;

public class Solution
{
    public static void Main(string[] args)
    {
        int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
        Array.Sort(arr);
        Console.WriteLine(string.Join(" ", arr));
    }
}

which I want to test in a separate project like

[TestMethod]
public void TwoNumbersDescendingAreSwapped()
{
     string input = "2 1";
     string expectedOutput = "1 2"; 
     // ... ???
     Assert.AreEqual(expectedOutput, actualOutput);
}

Is it possible to do that without actually using the .exe from Solution?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Move the code that does all the work in Main() to its own class and method:

public static class InputConverter
{
    public static string ConvertInput(string input)
    {
        int[] arr = Array.ConvertAll(input.Split(' '), int.Parse);
        Array.Sort(arr);
        return string.Join(" ", arr);        
    }
}

Your Main() then becomes:

public static void Main(string[] args)
{
    var input = Console.ReadLine();
    var output = InputConverter.ConvertInput(input);
    Console.WriteLine(output);
}

You can now test ConvertInput() without being dependent by the write and read functions of Console:

[TestMethod]
public void TwoNumbersDescendingAreSwapped()
{
    // Arrange
    var input = "2 1";
    var expectedOutput = "1 2"; 
    // Act
    var actualOutput = InputConverter.ConvertInput(input);
    // Assert
    Assert.AreEqual(expectedOutput, actualOutput);
}

As an aside: the way you are passing in your arguments seems as though you are guaranteeing that the input will always be what you expect it to be. What happens when the user passes in something totally different than string representations of integers? You need to validate the input in InputConverter.ConvertInput() and create appropriate courses of action based on that (throw an Exception, return null, depends on what you're after). You'll then have to unit test those scenarios as well to make sure ConvertInput() performs as expected for all cases.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda