Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
127 lines (110 sloc) 3.68 KB
---
layout: default
title: Fixie
---
<div class="one-third column">
<p><strong>Write some tests.</strong></p>
<p>In your test project, add a class whose name ends with 'Tests'.
The public methods are assumed to be tests.
This is the 'default convention'.</p>
</div>
<div class="two-thirds column">
{% highlight csharp %}
using Shouldly;
public class CalculatorTests
{
public void ShouldAdd()
{
var calculator = new Calculator();
calculator.Add(2, 3).ShouldBe(5);
}
public void ShouldSubtract()
{
var calculator = new Calculator();
calculator.Subtract(5, 3).ShouldBe(2);
}
}
{% endhighlight %}
</div>
<hr />
<div class="one-third column">
<p><strong>Tell Fixie how to find your tests.</strong></p>
<p>Let's say you don't like the default convention. You'd like to define your own convention to
support async setup steps whenever a test class contains a SetUp() method. To customize the
test discovery phase, subclass Discovery.</p>
<p>With this customization in place, a test class's SetUp() method will no longer be treated
as a test method.</p>
</div>
<div class="two-thirds column">
{% highlight csharp %}
using Fixie;
public class TestingConvention : Discovery
{
public TestingConvention()
{
Methods
.Where(x => x.Name != "SetUp");
}
}
{% endhighlight %}
</div>
<hr />
<div class="one-third column">
<p><strong>Tell Fixie how to run your tests.</strong></p>
<p>Next, we need to customize the test execution phase, calling the SetUp() method at an appropriate
moment in the test class lifecycle. To customize the test execution phase, implement Execution.</p>
</div>
<div class="two-thirds column">
{% highlight csharp %}
using Fixie;
public class TestingConvention : Discovery, Execution
{
public TestingConvention()
{
Methods
.Where(x => x.Name != "SetUp");
}
public void Execute(TestClass testClass)
{
testClass.RunCases(@case =>
{
var instance = testClass.Construct();
SetUp(instance);
@case.Execute(instance);
});
}
static void SetUp(object instance)
{
instance.GetType().GetMethod("SetUp")?.Execute(instance);
}
}
{% endhighlight %}
</div>
<hr />
<div class="one-third column">
<p><strong>Use your testing convention.</strong>
<p>Writes some tests that leverage your new convention.</p>
<p>A test class can now have a SetUp() method, and when present it will be called right before each test method executes.
If SetUp() is declared async, it'll be properly awaited before each test method executes. </p>
</div>
<div class="two-thirds column">
{% highlight csharp %}
public class CustomerSearcherTests
{
public async Task SetUp()
{
await SetUpSampleCustomersAsync(/* ... */);
}
public void ShouldGetCustomersByName()
{
var searcher = new CustomerSearcher();
searcher.SearchByName("Smith").Select(x => x.FullName).ShouldBe(new[] { "Sally Johnson", "John Smith" });
}
public void ShouldGetCustomersByAddress()
{
var searcher = new CustomerSearcher();
searcher.SearchByAddress("Elm Street").Select(x => x.FullName).ShouldBe(new[] { "Sally Johnson" });
}
}
{% endhighlight %}
</div>