Sitefinity web test runner: Create integration tests
The integration tests are structured in fixtures. Each fixture holds a set of tests and a tear down method. For more information about each element, read the sections below.
Test fixtures
The test fixtures hold the tests. You can use multiple fixtures and group your tests depending on the functionality they cover. Each fixture is represented by a class. To create a fixture, perform the following:
- Create an empty class file.
- Mark the class with the the TestFixture attribute.
- Mark the class with the Description attribute. (optional)
Here is a code example:
[TestFixture]
[Description(
"Some simple tests."
)]
public
class
SimpleTests
{
}
Tests
The tests are represented by methods that contain the logic that you want to test. To create a test method, you must perform the following:
- Create a method that returns void and do not accept any arguments.
- Mark the method with the Test attribute.
- Mark the method with others optional attributes:
- Write the logic that you want to test.
- Assert the results.
NOTE: You can mark a test method with the Ignore attribute. When you run an entire fixture or all of the tests, the tests marked with this attribute will get skipped.
Here is a code example containing a few tests:
[TestFixture]
[Description(
"Some simple tests."
)]
public
class
SimpleTests
{
[Test]
[Author(
"Telerik Developer"
)]
[Description(
"A simple test that will pass."
)]
public
void
PassingTest()
{
int
expectedValue = 2;
int
actualValue = 1 + 1;
Assert.AreEqual<
int
>(expectedValue, actualValue);
}
[Test]
[Author(
"Telerik Developer"
)]
[Description(
"A simple test that will fail."
)]
public
void
FailingTest()
{
int
expectedValue = 2;
int
actualValue = 1 + 1 + 1;
Assert.AreEqual<
int
>(expectedValue, actualValue);
}
[Test]
[Author(
"Telerik Developer"
)]
[Description(
"A simple test that will be ignored. You can run it manually in the Test runner."
)]
[Ignore]
public
void
IgnoredTest()
{
int
expectedValue = 2;
int
actualValue = 1 + 1;
Assert.AreEqual<
int
>(expectedValue, actualValue);
}
}
Tear down
The tear down method is executed after each test. It is optional. Typically you use it only when you have made some permanent changes to the testing environment and you want to bring them back once the test has been executed. For example if you have created a document library during your test and you want to remove it once the test has been executed, you implement the logic for removing the document library in the tear down method. To create a tear down method, perform the following:
- Create a method that returns void and do not accept any arguments.
- Mark the method with the TearDown attribute.
- Implement the logic for bringing the environment back to its original state.
Here is an example of a tear down method:
[TearDown()]
public
void
TearDown()
{
LibrariesManager librariesManager = LibrariesManager.GetManager();
var libraryId =
new
Guid(TestLibraryId);
var library = librariesManager.GetDocumentLibraries().SingleOrDefault(l => l.Id == libraryId);
if
(library !=
null
)
{
librariesManager.DeleteDocumentLibrary(library);
librariesManager.SaveChanges();
}
}