Tutorial by Examples

At its simplest, a unit test consists of three stages: Prepare the environment for the test Execute the code to be tested Validate the expected behaviour matches the observed behaviour These three stages are often called 'Arrange-Act-Assert', or 'Given-When-Then'. Below is example in C# tha...
Good unit tests are independent, but code often has dependencies. We use various kinds of test doubles to remove the dependencies for testing. One of the simplest test doubles is a stub. This is a function with a hard-coded return value called in place of the real-world dependency. // Test that ...
Classic unit tests test state, but it can be impossible to properly test methods whose behavior depends on other classes through state. We test these methods through interaction tests, which verify that the system under test correctly calls its collaborators. Since the collaborators have their own...
JUnit is the leading testing framework used for testing Java code. The class under test models a simple bank account, that charges a penalty when you go overdrawn. public class BankAccount { private int balance; public BankAccount(int i){ balance = i; } public Bank...
using NUnit.Framework; namespace MyModuleTests { [TestFixture] public class MyClassTests { [TestCase(1, "Hello", true)] [TestCase(2, "bye", false)] public void MyMethod_WhenCalledWithParameters_ReturnsExpected(int param1, string para...
import unittest def addition(*args): """ add two or more summands and return the sum """ if len(args) < 2: raise ValueError, 'at least two summands are needed' for ii in args: if not isinstance(ii, (int, long, float, compl...
using Xunit; public class SimpleCalculatorTests { [Theory] [InlineData(0, 0, 0, true)] [InlineData(1, 1, 2, true)] [InlineData(1, 1, 3, false)] public void Add_PassMultipleParameters_VerifyExpected( int inputX, int inputY, int expected, bool isExpectedCorrect) ...

Page 1 of 1