-
Notifications
You must be signed in to change notification settings - Fork 0
Framework Basics
Note: To use the Edison test features, you will need to reference the Edison.Framework.dll
library in your project. You can do this by either referencing the dll after installing via Chocolatey, or you can reference the library via NuGet.
Writing tests in Edison is simple.
Let's say we wish to write a test that ensures 1 + 1
does indeed equal 2
. The first thing we have to do is create a normal C# class however, this class will be decorated with the [TestFixture]
attribute. The [TestFixture]
attribute allows Edison to locate classes that may contain tests that need running, and can be found in the Edison.Framework
library:
[TestFixture]
public class BasicTest
{
}
Next we need to create our test. To do this we create a normal method, but we decorate it with the [Test]
attribute - also found in the Edison.Framework
library. The [Test]
attribute signifies to Edison that this method is a test that we wish to have run. Note however, that any method marked with the attribute, but the class is not decorated with the [TestFixture]
attribute, will not be picked up by Edison.
Furthermore, tests cannot return values. They must be void:
[TestFixture]
public class BasicTest
{
[Test]
public void EqualityTest()
{
}
}
So far we have a class which is a TestFixture, and contains one Test for Edison to run. Now we just need to insert a basic assert check to see if 1 + 1
does indeed equal 2
.
Edison comes bundled with an assert library, in fact it also allows you to completely override the inbuilt assert library. In Edison, all assert checks are accessed via the AssertFactory
class. This class is lazily initialised through AssertFactory.Instance
, and returns an instance of an IAssert
.
On AssertFactory.Instance
's first call, this will create and return and instance of the inbuilt Assert
class logic. If you wish to use you own custom assert logic, then you can create an new class inheriting from IAssert
and set it against the AssertFactory.Instance
property.
However, we're just using the inbuilt assert logic, so this is what our test now looks like:
[TestFixture]
public class BasicTest
{
[Test]
public void EqualityTest()
{
AssertFactory.Instance.AreEqual(1 + 1, 2, "Oh noes, they aren't equal!");
}
}
You now have a test that is ready to be run. If the assert passes, then the test will pass. However if the assert fails, then the test will also fail and the error message "Oh noes, they aren't equal!"
will be displayed. To run tests, see the Console or GUI sections.
When you're running tests, it might be necessary to have some initial setup logic that every test needs. Conversely there may also be some teardown/cleanup logic that every test needs to run after completing regardless if success or fail.
To run some setup logic before every test you can use the [Setup]
attribute. Any logic within this method will be run every single time before a test is run:
[TestFixture]
public class BasicTest
{
[Setup]
public void Setup()
{
// any logic here will be called before every test runs in this fixture
}
[Test]
public void EqualityTest()
{
AssertFactory.Instance.AreEqual(1 + 1, 2, "Oh noes, they aren't equal!");
}
}
To run some teardown logic after every test you can use the [Teardown]
attribute. Any logic within this method will be run every single time after a test is run, regardless if the test was a success, fail or error:
[TestFixture]
public class BasicTest
{
[Teardown]
public void Teardown()
{
// any logic here will be called after every test runs in this fixture
}
[Test]
public void EqualityTest()
{
AssertFactory.Instance.AreEqual(1 + 1, 2, "Oh noes, they aren't equal!");
}
}
Like when running tests, it might be necessary to have some initial setup logic that needs to run at the start of a fixture. Conversely there may also be some teardown/cleanup logic that a fixture needs to run after all of its tests have completed.
To run some setup logic at the start of a fixture you can use the [TestFixtureSetup]
attribute. Any logic within this method will be run once at the start of a fixture run:
[TestFixture]
public class BasicTest
{
[TestFixtureSetup]
public void TestFixtureSetup()
{
// any logic here will be called once at the start of the fixture
}
[Test]
public void EqualityTest()
{
AssertFactory.Instance.AreEqual(1 + 1, 2, "Oh noes, they aren't equal!");
}
}
To run some teardown logic after every test has completed can use the [TestFixtureTeardown]
attribute. Any logic within this method will be run once, after all tests have completed:
[TestFixture]
public class BasicTest
{
[TestFixtureTeardown]
public void TestFixtureTeardown()
{
// any logic here will be called once, after all tests have completed in this fixture
}
[Test]
public void EqualityTest()
{
AssertFactory.Instance.AreEqual(1 + 1, 2, "Oh noes, they aren't equal!");
}
}