Użyj NUnit Assert.Metoda Throws czy atrybut ExpectedException?

Odkryłem, że są to dwa główne sposoby testowania WYJĄTKÓW:

Assert.Throws<Exception>(()=>MethodThatThrows());

[ExpectedException(typeof(Exception))]
Który z nich byłby najlepszy? Czy jedna oferuje przewagę nad drugą? Czy jest to po prostu kwestia osobistych preferencji?
Author: DavidRR, 2013-02-22

5 answers

Pierwszy pozwala na testowanie więcej niż jednego wyjątku, z wieloma wywołaniami:

Assert.Throws(()=>MethodThatThrows());
Assert.Throws(()=>Method2ThatThrows());

Drugi pozwala na testowanie tylko jednego wyjątku dla każdej funkcji testowej.

 77
Author: chue x,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-21 23:59:53

Główna różnica to:

ExpectedException() atrybut sprawia, że test przechodzi pomyślnie, jeśli wyjątek występuje w dowolnym miejscu w metodzie testowej.
Użycie Assert.Throws() pozwala określić exact miejsce kodu, w którym spodziewany jest wyjątek.

NUnit 3.0 całkowicie rezygnuje z oficjalnego wsparcia dla ExpectedException.

Więc zdecydowanie wolę używać metody Assert.Throws() zamiast atrybutu ExpectedException().

 237
Author: Alexander Stepaniuk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-18 08:51:48

[[1]] ja wolęrzuca, ponieważ pozwala mi to zweryfikować i potwierdzić inne warunki po wyrzuceniu wyjątku.

    [Test]
    [Category("Slow")]
    public void IsValidLogFileName_nullFileName_ThrowsExcpetion()
    {
        // the exception we expect thrown from the IsValidFileName method
        var ex = Assert.Throws<ArgumentNullException>(() => a.IsValidLogFileName(""));

        // now we can test the exception itself
        Assert.That(ex.Message == "Blah");

    }
 32
Author: Mike Parkhill,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-04 02:19:10

Możesz również wpisać błąd, którego oczekujesz (jak w starej wersji attrib).

Assert.Throws<System.InvalidOperationException>(() => breakingAction())
 9
Author: Reverend Sfinks,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-24 12:23:41

Jeśli używasz starszej wersji() z NUnit Następnie należy użyć ExpectedException.

Jeśli używasz 2.5 lub nowszą wersję możesz użyć Assert.Throw()

Https://github.com/nunit/docs/wiki/Breaking-Changes

Jak używać: https://www.nunit.org/index.php?p=exceptionAsserts&r=2.5

 0
Author: Gireesh k,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-22 09:13:58