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

129
Vistas
What is the preferred way to handle multiple test cases in Xunit?

I have migrated to using Xunit for unit tests from NUnit. With NUnit, I would create one method with multiple test cases that have the same outcome. For example, the following NUnit unit test tests the validation of a class constructor, specifically the "name" variable. The name can't be null, empty or whitespace. The test checks that an ArgumentNullException is correctly thrown:

    [Test]
    [TestCase(null)]
    [TestCase("")]
    [TestCase("     ")]
    [ExpectedException(typeof(ArgumentNullException))]
    public void Constructor_InvalidName_ExceptionThrown(string name)
    {
        // action
        make_Foo(name);
    }

    private make_Foo(string name)
    {
        return new Foo(name);
    }

This is how I have implemented this using Xunit:

    [Fact]
    public void Constructor_InvalidName_ExceptionThrown()
    {
        Assert.Throws<ArgumentNullException>(() => new Foo(null));
        Assert.Throws<ArgumentNullException>(() => new Foo(""));
        Assert.Throws<ArgumentNullException>(() => new Foo("    "));
    }

This seems bad for two reasons - I have multiple Asserts in what is supposed to be a "unit" test, and with the test cases buried within the method (which could get a lot more complicated in some of the other unit tests).

What is the preferred way to handle multiple test cases in Xunit?

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

0

You can use the Theory attribute to the same effect:

[Theory()]
[InlineData(null)]
[InlineData("")]
[InlineData("     ")]
public void Constructor_InvalidName_ExceptionThrown(string name)
{
    Assert.Throws<ArgumentNullException>(() => new Foo(name));
}

I am not sure if xUnit has am attribute equivalent to ExpectedException however. If there is, I would not use it.

There used to be an ExpectedException attribute in xUnit but it has since been deprecated in favour of Assert.Throws.

over 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