Showing posts with label Gallio. Show all posts
Showing posts with label Gallio. Show all posts

2012/07/23

Announcing Gallio and MbUnit 3.4

I just realized that Graham Hay made a new release of Gallio/MbUnit a few days ago. That new version supports R#6.1 and the latest versions of several unit testing frameworks. Please read the release notes for more details.

· Downloads

Please visit the Gallio website Downloads page to get the binaries, or grab them directly from here:

2011/12/23

Dynamic binding for data-driven tests in MbUnit v3

MbUnit v3.3 has been now released for a while and I realize that I have completely forgotten to write about the new nifty features added by our fellow contributor Aleksandr Jones. Aleks had added a couple of very powerful attributes for writing data-driven tests. He had even written some useful documentation in the Gallio wiki. Those attributes provide a seamless way to bind automatically your external data sources (CSV or XML data files) to the test parameters.

Basically you can declare your test parameters as "dynamic" variables and let MbUnit to bind automagically the parameters behind the scene. Of course it also means that the feature is only available for test projects targeting the .NET 4 framework.

Let's consider the following simple comma-separated example data file:

Let's now write a test method which loads the file and gets data from it. We could have used the classic [CsvData] attribute to bind explicitly the parameters. But with the new [FlatFileDataObject] attribute, it's far easier:
Look at how the different properties of the test parameter were bound and printed in the test log.
That's just awesome! Good work Aleks!

2011/12/19

Web Testing with MbUnit and WatiN

A serie of articles of mine have been published recently on developerFusion.com. The articles explain how to use efficiently Gallio/MbUnit and WatiN to test your web applications. You can find already many interesting articles about web testing here and there. What I tried to do is to focus on a few less treated problems like testing on the local host, web pages with AJAX requests, controlling the Visual Studio web application server, etc. Good reading!
A big thank you to Dan Maharry and all the editorial team of developerFusion.com for having reviewed my articles.

2011/10/14

Announcing Gallio and MbUnit 3.3.1

We are pleased to announce that a new release of Gallio/MbUnit is now available. Thanks to an awesome work made by Graham Hay, we can finally release a version which supports Resharper 6. Please see below or read the release notes for more details.

· Downloads

Please visit the Gallio website Downloads page to get the binaries, or grab them directly from here:

2011/09/17

Announcing Gallio and MbUnit 3.3

We are pleased to announce that a new release of Gallio/MbUnit is now available. This release contains many new features and improvements for MbUnit. Please see below or read the release notes for more details.

· Downloads

Please visit the Gallio website Downloads page to get the binaries, or grab them directly from here:

2011/06/24

Running Tests Under Another User in Gallio/MbUnit v3.3

It may be convenient sometimes to run test methods under a user account other than the one running the current test session. For example, you might want to test some security feature and see if it behaves correctly according to the level of credentials of a particular archetypal user.

MbUnit provides a very simple attribute that does exactly that: ImpersonateAttribute. To use it, decorate the test methods (or the entire test fixture) with one or several instances of that attribute and feed them with valid user names, passwords and optionally a domain.

You can find more details and a few samples on the Gallio Wiki.

2011/06/15

Extending MbUnit With Custom Expected Exception Attributes

MbUnit provides several ways to deal with expected exceptions in the user code under test. The most popular one is certainly to decorate the test method with an [ExpectedException] attribute:
[Test, ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Constructs_Foo_with_negative_value_should_throw_exception()
{
new Foo(-123);
}
Conveniently, you can use some built-in attributes to save a few more keystrokes:
[Test, ExpectedArgumentOutOfRangeException]
public void Constructs_Foo_with_negative_value_should_throw_exception()
{
new Foo(-123);
}
Those shortcut attributes are very useful. They significantly improve the readability of the tests by removing two pairs of noisy nested parenthesis.

But what if you want to define your own shortcut attribute for a custom exception of yours? Imagine for example that you use a fancy SpaceTimeBrokenException all over your code. Let's define a custom shortcut expected exception attribute for it. That's very easy with Gallio's extensibility model: you just need to derive from ExceptedExceptionAttribute like this:
using Gallio.Framework.Pattern;
using MbUnit.Framework;

[AttributeUsage(PatternAttributeTargets.Test, AllowMultiple = false, Inherited = true)]
public class ExpectedSpaceTimeBrokenExceptionAttribute : ExpectedExceptionAttribute
{
public ExpectedSpaceTimeBrokenExceptionAttribute()
: base(typeof(SpaceTimeBrokenException))
{
}

public ExpectedSpaceTimeBrokenExceptionAttribute(string message)
: base(typeof(SpaceTimeBrokenException), message)
{
}
}
That's all! Now just put that new class in a namespace accessible from your test project and enjoy it...
[Test, ExpectedSpaceTimeBrokenException]
public void Run_TimeMachine_with_negative_power_should_collapse_the_entire_universe()
{
var timeMachine = new TimeMachine();
timeMachine.Run(-1E10);
}

2011/05/10

More About Custom Data Source Attributes

Some weeks ago, I explained how to extend Gallio/MbUnit with custom data source attributes. The principle is to provide convenient and reusable attributes which feed test methods with data coming from an external source (e.g. file, database, distant service, etc.)

Aleksandr Jones has just written a very nice article in his blog that illustrates the concept. He explains how to create an attribute that grabs test parameters from an OLE/SQL database. The typical usage is the following:
[Test]
[CustomAttribute("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MikeGarage.accdb;Persist Security Info=False;", "Customers")]
public void CustomAttributeTest(DataRow dataRow)
{
// ...
}

2011/03/01

Extending MbUnit with custom attributes

In a recent post, Krzysztof Kozmic discussed about an efficient way to link a particular test to an issue in a bug tracker. MbUnit provides several useful metadata attributes such as [Category], [Author], [TestsOn] etc. But none is about issue tracking :( Fortunately, the Gallio framework is easily extensible (did I say that already?); let's create such an attribute.

Custom Metadata Attribute

We will first create a new simple metadata attribute. We just need to derive from Gallio.Framework.Pattern.MetadataPatternAttribute and to implement some basic code logic in it.
[AttributeUsage(PatternAttributeTargets.TestComponent, AllowMultiple = true, Inherited = true)]
public class IssueAttribute : MetadataPatternAttribute
{
private readonly int issue;

public IssueAttribute(int issue)
{
this.issue = issue;
}

public int Issue
{
get { return issue; }
}

protected override IEnumerable<KeyValuePair<string, string>> GetMetadata()
{
yield return new KeyValuePair<string, string>("Issue", issue.ToString());
}
}
We can now use that simple attribute and enjoy the information displayed in the test report.
[TestFixture]
public class MyTestFixture
{
[Test, Issue(123456)]
public void MyTest()
{
}
}

Custom Test Decorator

But wait! We can certainly do better. My favorite issue tracker is a fancy web application and I would like to get an hyperlink that leads me directly to that issue. We cannot use Gallio metadata for that purpose because they are only key/value pairs of strings. Therefore it's uneasy to make them hold more interesting data like an URL. We will create a new test decorator instead; and use the powerful test log API to display cool stuff in the report. Let's implement a new test decorator attribute (MbUnit.Framework.TestDecoratorAttribute)
[AttributeUsage(PatternAttributeTargets.Test, AllowMultiple = true, Inherited = true)]
public class IssueAttribute : TestDecoratorAttribute
{
private readonly int issue;

public IssueAttribute(int issue)
{
this.issue = issue;
}

public int Issue
{
get { return issue; }
}

protected override void Execute(PatternTestInstanceState testInstanceState)
{
using (TestLog.BeginSection("Issue"))
{
TestLog.Write("This test is related to the ");
using (TestLog.BeginMarker(Marker.Link("http://MyCoolBugTracker/Issue/" + issue)))
{
TestLog.WriteLine("issue #" + issue);
}
}

base.Execute(testInstanceState);
}
}
Look now at the cool link printed in the test report:

2011/02/28

Announcing Gallio and MbUnit v3.2.3

We are pleased to announce that a new release of Gallio/MbUnit is now available. This release contains many new features and improvements for MbUnit. Please see below or read the release notes for more details.

· Downloads

Please visit the Gallio website Downloads page to get the binaries, or grab them directly from here:

· Overview

This release mainly focuses on new features for MbUnit.
NHamcrest
NHamcrest is an idiomatic C# port of Hamcrest (java version) made by Graham Hay. It provides a nice fluent syntax for Assert.That. The library is now part of the Gallio bundle.
[Test]
public void Assert_that()
{
Assert.That(1, Is.Anything());

Assert.That(1 + 1, Is.EqualTo(2));
Assert.That("string", Is.EqualTo("string"));
Assert.That(5.0, Is.EqualTo(5.0));

Assert.That(false, Is.False());
Assert.That(true, Is.True());

Assert.That(null, Is.Null());
Assert.That("", Is.NotNull());

Assert.That(5, Is.LessThan(6));
Assert.That(5, Is.GreaterThan(4));

const string aString = "aString";
Assert.That(aString, Is.SameAs(aString));
Assert.That("the cat sat on the mat", Starts.With("the cat"));
Assert.That("the cat sat on the mat", Contains.String("sat on"));
Assert.That("the cat sat on the mat", Ends.With("the mat"));

Assert.That(1, Is.InstanceOf<int>());

Assert.That(() => { throw new ArgumentException(); }, Throws.An<ArgumentException>());
}
Assert.HasAttribute(s)
2 new assertions to check whether a code element is decorated with a specific attribute. Those assertions are particularly powerful when used in combination with the Mirror API.
[DisplayName("Item")]
public class Product
{
public int Id { get; set; }

[Required, StringLength(10)]
public string Name { get; set; }

[Required]
public string Description { get; set; }

[Required, Range(0, 1000)]
public decimal Price { get; set; }
}
[TestFixture]
public class ProductTest
{
[Test]
public void Product_has_DisplayNameAttribute()
{
Assert.HasAttribute<DisplayNameAttribute>(typeof(Product));
}

[Test]
public void Description_has_RequiredAttribute()
{
var property = Mirror.ForType<Product>()["Description"];
Assert.HasAttribute<RequiredAttribute>(property);
}
}
Case insensitive string matching.
Assert.Contains, Assert.DoesNotContain, Assert.StartsWith, and Assert.EndsWith now support case insensitive string matching.
[Test]
public void Assert_contains()
{
string actualValue = "Edgar Allan Poe (January 19, 1809 – October 7, 1849)";
string expectedSubstring = "ALLAN POE";
Assert.Contains(actualValue, expectedSubstring, StringComparison.OrdinalIgnoreCase);
}
Assert.DoesNotExist
Again a new assertion which finally brings some symmetry to Assert.Exists.
[Test]
public void Assert_doesNotExist()
{
var data = new[] { "Athos", "Porthos", "Aramis" };
Assert.DoesNotExist(data, x => x.Contains("gnan"));
}
Multiple [Impersonate] instances
Running tests under another identity is a little-known feature of MbUnit. It is now possible to provide several user credentials at once.
[Test]
[Impersonate(UserName = "Julius", Password = "Ven1V1d1V1c1")]
[Impersonate(UserName = "Brutus", Password = "F**kD4ddy")]
public void Test()
{
var identity = WindowsIdentity.GetCurrent();
TestLog.Write("User = {0}", identity.Name);
}
And many other things...
Such as improvements to contract verifiers, "Seed" property to the random data sources, easier ways to assert over inner exceptions, etc.

· Credits

This great release was made possible thanks to the hard work of our dedicated fellow contributors:
  • Graham Hay
  • Artur Zgodziński
  • Jonathan Linstead
  • Yann Trévin
And a special thank to Jeff Brown for his invaluable advices and his deep knowledge of the Gallio infrastructure.

Finally, we would like to express our gratitude to our passionate users who help us in building that great piece of software by submitting bug reports, patches, blog posts, and suggestions.

And more than ever, contributions are gratefully received.

2011/02/18

Multi-page Reports in Gallio/MbUnit v3.3

While the announcement of a new intermediate release of Gallio/MbUnit is imminent (next week?), we are continuing to focus on the major improvements that will be available in the future versions.

In case you are running Gallio against very large test suites (> 10000 tests), you might be interested by the following new feature. You can experiment it already by installing the latest v3.3 daily build (it's pretty stable already, IMHO)

As you may know, we dropped the XSL-based engine used for the report generation, in favor of high performing VTL templates (based on Castle NVelocity). The generation of the Text/HTML reports (Text, Text-Condensed, Html, Html-Condensed, XHtml, and XHtml-Condensed) is about 3 times faster. But more important, the report writer now supports multi-paging. Say goodbye to the heavy 20Mb reports that take hours to open in the web browser! Gallio is now able to split large reports into several smaller browsable files.



You might configure some parameters from the Gallio Control Panel. It's machine-wide settings that will affect the reports generated by every tool and test runner.

2011/01/19

What's next?

Gallio/MbUnit v3.2.2 was released some days ago. And we've got many "thank you" for the resolution of this annoying CS1685 issue (thank you Graham for having fixed it).

Now what can you expect from the incoming releases of Gallio? The path to v3.3 is still long, but we plan to release several intermediate point releases with more fixes, more improvements and a lot of new features. In a random order, here is an overview of those future new nifty features:
  • Major performance improvements: We are in the process of replacing the XSLT-based engine which generates the test reports by a brand new and faster system based on Castle NVelocity. We also would like to do no longer rely on that damn slow .NET Remoting for IPC communications and to use the popular Google's Protobuf format instead. Those major changes should bring dramatic performance improvements while running the tests and formatting the test reports.
  • MbUnitCpp: a port of the MbUnit testing framework to the native unmanaged C++ realm; entirely integrated to the Gallio ecosystem. It’s not feature complete yet, but MbUnitCpp is already fully functional and available as part of the daily v3.3 builds. The wiki documentation is also very detailed already; with a comprehensive tutorial.
  • NHamcrest support: Graham Hay did port recently Hamcrest to C#. Assert.That is going to be extended to support that fluent API.
  • PartCover support: A new adapter for this excellent test coverage tool.
  • New assertions, new contract verifiers, and many other little enhancements (see draft release notes for detailed)
Some of those features are in fact fully or partially available in the v3.3 trunk already. Feel free to test them. Feedback and suggestions are always gratefully received.

And of course, we will continue to closely follow the continuous releases of the 3rd party tools that coexist in the Gallio ecosystem and to update the existing test adapters and plugins accordingly.

2011/01/13

Announcing Gallio and MbUnit v3.2.2

We are pleased to announce that a maintenance release of Gallio/MbUnit is now available. This release mainly contains many bug fixes and little enhancements. It fixes in particular this annoying CS1685 warning. Read the release notes for more details.

Please visit the Gallio website Downloads page to get the binaries, or grab them directly from here:

2010/12/03

Announcing Gallio and MbUnit v3.2.1

We are pleased to announce that a maintenance release of Gallio/MbUnit is now available. This release mainly contains many bug fixes and little enhancements. But it also features the long-awaited support to Jetbrains dotCover. See the release notes for more details.

Please visit the Gallio website Downloads page to get the binaries, or grab them directly from here:

2010/11/15

MbUnit v3 to support (N)Hamcrest

Recently, Graham Hay did port the well-known Hamcrest library to .NET. The new version of that library was judiciously ;) named NHamcrest. It is also fully integrated to Gallio/MbUnit with Assert.That.

Try it out by downloading the latest v3.3 daily build and give us feedback and suggestions.
[Test]
public void NHamcrestExample()
{
var array = new[] {"red", "green", "blue"};
Assert.That(array, Is.InstanceOf(typeof(string[])));
Assert.That(array, Has.Items(new[] { "green", "red" }));
Assert.That(array, Has.Item<string>(Starts.With("bl")));
}

2010/10/07

Testing Custom Data Source Attributes in MbUnit v3.2

In a recent post, I explained how to create a custom data source attribute for MbUnit. But before using it, it's certainly safer to test it. The method described below is the same as the one which is applied in the MbUnit test project itself. It is widely used to verify that the built-in attributes of MbUnit behave as expected. The principle is the following:
  1. Create a nested explicit sample test fixture which consumes the attribute under test. It must be marked as explicit, so that it will not be taken in account by the primary test runner.
  2. Create a regular unit test which launches an inner isolated test runner, and runs the sample fixture.
  3. Retrieve the output of the inner test runner and assert over the test log.
The Gallio framework has everything you need to create an isolated test runner. But in order to make the things easier, the Gallio SDK (you can find the SDK under %gallio_install_path%\sdk) contains a couple of handy helper classes for that very purpose. It provides in particular a BaseTestWithSampleRunner class that you can use as a base class of you main fixture, and a [RunSample] attribute to easily target the nested explicit sample fixtures.

Here is a simple example that shows how to test the [BooleanData] attribute that we did create last time.
[TestFixture, RunSample(typeof(SampleFixture))]
public class BooleanDataAttributeTest : BaseTestWithSampleRunner
{
[Test]
public void Test()
{
var runs = GetTestStepRuns(typeof(SampleFixture), "Test");
var logs = runs.Select(GetLog).Where(x => x.Length > 0);
Assert.AreElementsEqualIgnoringOrder(new[] { "value=True", "value=False" }, logs);
}

[TestFixture, Explicit]
internal class SampleFixture
{
[Test]
public void Test([BooleanData] bool value)
{
TestLog.Write("value={0}", value);
}
}
}
Want to know more? Be sure to read this page in the Gallio wiki.

2010/09/22

Announcing Gallio and MbUnit v3.2

We are pleased to announce the release of Gallio & MbUnit v3.2. Please visit the Gallio website download page. This is a major release with many new features and enhancements. See the release notes for more details.

Be sure to read Andy's blog post too.

2010/09/03

Writing Custom Data Source Attributes in MbUnit v3.2

The extensibility of the Gallio platform is simply amazing. You can virtually extend any part of the system. It goes from a simple plugin that provides new handy functionalities to a full-blown adapter for your fancy testing framework. Today, I would like to explain how easy it is to extend the MbUnit framework with a custom data source attribute.

MbUnit has many useful built-in data source attributes which might be used to create powerful data-driven tests. The most popular attributes are certainly [Row] and [Column].

Now let's imagine you get bored with writing that kind of tests:
[Test]
public void Mytest(
[Column(true, false)] bool flag1,
[Column(true, false)] bool flag2,
[Column(true, false)] bool flag3)
{
// ...
}
Imagine how beautiful would be the world if you could write the following instead:
[Test]
public void Mytest(
[BooleanData] bool flag1,
[BooleanData] bool flag2,
[BooleanData] bool flag3)
{
// ...
}
Unfortunately, this data source attribute does not exist in MbUnit.

So let's make it happen!

Creating a custom data source is very easy. Basically you simply need to derive from MbUnit.Framework.DataAttribute, and to override the virtual method PopulateDataSource.
[AttributeUsage(PatternAttributeTargets.DataContext, AllowMultiple = false, Inherited = true)]
public class BooleanDataAttribute : DataAttribute
{
protected override void PopulateDataSource(IPatternScope scope, DataSource dataSource, ICodeElementInfo codeElement)
{
dataSource.AddDataSet(new ValueSequenceDataSet(new object[] { true, false }, GetMetadata(), false));
}
}
That's all. Compile and run your test happily!

You might find more inspiration by examining the actual implementation of existing built-in attributes such as [EnumData] or [RandomStrings] which is slightly more complicated as it relies on the underlying Gallio data generation framework.

Next time, I will explain how to properly test you custom data source attribute by using the Gallio SDK.

2010/08/31

Data-Driven Testing in MbUnit v3

MbUnit was mostly known in the past as the testing framework dedicated to data-driven testing. This is certainly due to the very popular [Row] attribute. But unsurprisingly this statement is still true in version 3 as the support for data-driven testing is still certainly one the most powerful feature of Gallio/MbUnit v3.

But the famous [Row] attribute is not the only way to create data-driven tests. We have written several articles and tutorials in the Gallio Wiki which cover that wide topic. All the pages have not been written yet, but you will find many useful samples and details already. They explain how to use the [Row], [Column] and [Factory] attributes, how to combine them and how to flavor them with tasty parameters. In the next coming weeks, you will read more about external and generative data sources as well.

2010/07/18

Assert.Count in MbUnit v3.2

Assert.Count is a last-minute addition made to Gallio/MbUnit v3.2 just before passing to RC. Be sure to read the nice article that Vadim Kreynin has written about that new convenient assertion.