NUnit is a unit testing framework for .NET that allows developers to write and run repeatable tests to determine whether code is functioning as intended. Key features of NUnit include support for multiple test assemblies and configuration files, attributes and assertions to structure tests and validate results, and the ability to group tests into categories and run them in isolated test environments using theories and data-driven approaches.
4. NUnit
Multiple Assemblies
Configuration Files
Useful Test Cases
Assertions
When categories are used, only the
tests in the selected categories will be
run.
■ TestFixture Syntax
[TestFixture]
[Category LongRunning“)]
public class Tests
■ Test Syntax
Test]
[Category Long“)]
public void VeryLongTest
Categories Categories
Test Environment
Theory
5. NUnit
Multiple Assemblies
Configuration Files
Useful Test Cases
Assertions
In addition to running tests in a single
assembly, NUnit provides support for
tests organized as multiple assemblies
and for creating and running tests as
NUnit Test Projects
Categories
Multiple Assemblies
Test Environment
Theory
6. NUnit
Multiple Assemblies
Configuration Files
Useful Test Cases
Assertions
If your application stores settings in
configuration files, NUnit provides
you with the ability to have settings
for your test, which are different
from those used in production.
Categories
Configuration Files
Test Environment
Theory
7. NUnit
Multiple Assemblies
Configuration Files
Useful Test Cases
Assertions
You can use many attributes to run
a single test with different
parameters:
■ TestCase
■ A single set of parameters per attribute.
■ Range, Random, Values
■ Multiple simple-typed parameters.
■ TestCaseSource,
ValueSource
■ Use a public property or a void Method to
transfer parameters.
Categories Useful Test
Cases
Test Environment
Theory
8. NUnit
Multiple Assemblies
Configuration Files
Useful Test Cases
Assertions
NUnit Provide many types of
assertions:
■ Equality
■ AreEqual, AreNotEqual
■ Identity
■ AreSame, AreNotSame, Contains
■ Condition
■ IsTure, IsFalse, IsNull, IsNaN, IsEmpty,
IsNotEmpty
■ Comparison
■ Greater, Less, IsIsnstanceOf, IsAssignableFrom
■ CustomAsserts
■ That, FluentAssertions
■ Strings, Exceptions, You name it!
Categories
Assertions
Test Environment
Theory
9. NUnit
Multiple Assemblies
Configuration Files
Useful Test Cases
Assertions
There are some useful attributes that
affect the test Environment:
■ Ignore & Explicit
■ Both tests will not be run, though Explicit tests
can be ran when you run them yourself
■ Platform
■ used to specify platforms for which a test or
fixture should be run.
■ RequiresThread
■ used to indicate that a test method, class or
assembly should be run on a separate thread
Categories
Test
Environment
Test Environment
Theory
10. NUnit
Multiple Assemblies
Configuration Files
Useful Test Cases
Assertions
A Theory is a special type of test,
used to verify a general statement
about the system under development:
■ Assume & DataPoint
■ Theories are base on assumptions and
facts(DataPoints), opposing to normal tests that
are based on examples.
■ Assertions
■ If any Assertion fails, the Theory itself fails.
■ If at least some cases pass the
stated assumptions, and there
are no assertion failures or
exceptions, then the Theory passes
Categories
Theory
Test Environment
Theory
Today We will talk about the features and unique aspects of the NUnit Framework.*To those of you who are totally new to NUnit*NUnit is an open source unit testing framework for Microsoft .NET.It serves the same purpose as JUnit does in the Java world, and is one of many programs in the xUnit family.
So, except for the basic functionality of the framework (for an example running a test, having a test class/ fixture , etc…) it has some really powerful features.Some of the features are unique to NUnit.Some are also implemented in other frameworks but not as fluent and easy to use as they are in NUnit.In this presentation, I will talk about the most powerful features of the framework.
Transition
NUnit allows tests to be categorized by topic.Instead of running a test session based on a test fixture, why not run tests by categories?For an example:If I want to run only the tests that test “Edge Case” Issues, and I have categorized the tests as such, I don’t need to add ignore attributes on tests.I will simply need to specify the “Edge Case” Category to be ran and all the other tests will be excluded from the session.It can be either applied on a single test case or a whole test fixture!Plus, it makes the test class more readable if you can nest it under a certain topic.
A Brilliant feature is the ability to run tests from multiple assemblies at once.Most of you will not find it that interesting.But! Think of it. You want to check the integration of few components and need to test it.It allows you to run the tests of each component and the integrative test between them all at a single session!
This is a feature that also exists in MSTest but the big difference is that NUnit does it better.Faster Reading of Configuration, Better presentation of parameterized tests, and many more advantages.
NUnit provides many neat test case sources- whether it’s a single test case, a range of values or simply cases read from an external source.You can read more about it at Luke Wickstead – How To NUnit: Features @ http://lukewickstead.wordpress.com/2013/02/09/howto-nunit-features/
One of the more fluent and readable advantages of NUnit is the assertions.NUnit has many types of assertions that allow easy and very easy to understand assertions.There are also some add-ins that allow truly fluent assertionFind out more on Luke Wickstead – How To NUnit: Assertions @ http://lukewickstead.wordpress.com/2013/02/09/howto-nunit-assertions/
NUnit also allows you to control the test environment without any need to change the test itself.You may either ignore or decide whether the test should be ran (Ignore and Explicit).You may also choose the platform that the test will be ran at (for list of supported platforms, go to http://www.nunit.org/index.php?p=platform&r=2.2.10)And even require a test to be ran on a different thread so the tests will work in parallel. (default, the tests run on a single thread.)
Theories is a very complex issue.Apparently, it seems as the theory is a normal test. But it is a very special test.The theory is based on assumptions and data points that allow you to test a theoretical matter, rather then an actual fact that has examples.NUnit have a really good example for it on their website @ http://nunit.org/index.php?p=theory&r=2.5
That’s it for today people!If you want to ask me questions, simply mail me to Shirbr510@gmail.com and write in the subject “NUnit Presentation Questions”Till Next Time, Shir.