Showing posts with label Testing. Show all posts
Showing posts with label Testing. Show all posts

Sunday, January 4, 2015

Unit testing smells: The method is not public

A recurring pattern emerge often when unit testing, you find an interesting method you want to test but that method is private. Of course you could call that method through other public methods on the class but it might not be easy to cover all cases. This usually is a design smell for a Single Responsibility Principle violation. More on this later.

For now, to properly test the private method we have the following options:
  • Use reflection
  • Change the method visibility to public or internal
  • Extract the method to another class

Again, if you previously read my other post on non public class and inaccessible constructor you know you should avoid using reflection in tests.

Let's take a look at the other options

Change the method visibility to public or internal

The first thing we should do is to challenge why that method is private in the first place. Why can't we just change its visibility to public or internal (and use the InternalsVisibleTo attribute)?

This breaks encapsulation of the class and will let developers call the method directly in production code. That might not be what you intended to do but can help you in the short term. Abusing this technique will burn you in the long run so be careful with it.

Fortunately, there is a simpler way: extract the method to another class.

Extract the method to another class

If you are lucky, the method you want to extract is static and don't call any other method on the original class. You should be able to extract that method to a static utility class.

But what to do when the method calls other methods and use fields or properties on the class? For that we must analyze the class to extract responsibilities using the Single Responsibility Principle.

Single Responsibility Principle

Can you tell what is the purpose of your class in one sentence without using words like: and, but, also, or, etc.? If not then your class is doing more than one thing. Each segments of the sentence could be in different classes that only have one responsibility each. If the method you want to extract use the same fields than other methods you may want to push them by parameter instead before extraction to the other class.

Find more about the Single Responsibility Principle in an old post of mine.

Saturday, October 4, 2014

Unit testing smells: The class constructor is not easy to call

When we want to test an instance method on a class the first challenge is to create an instance of that class. Hopefully the class constructor is easy to call but that's not always the case.

Let's review a few cases where the constructor might prevent us to write tests easily:
  • The constructor is private or internal
  • It requires too many arguments or it is too hard to create/provide those arguments
  • The constructor calls external dependencies

The case of the internal or private constructor

For this we can follow the same advices than with testing non-public class. But whatever you do please don't use reflection to call the constructor.

Also stated in testing non-public class, you should ask yourself if it would be better to test this class via another public class that use it. A private constructor usually means that there is a factory somewhere you should use to instantiate the class. You should figure out a way to use that factory or redesign it in case you have difficulties to work with it in unit tests.

When dealing with internal access modifier I usually use the InternalVisibleTo attribute for my test project.

[assembly: InternalsVisibleTo("Contoso.MyApp.UnitTests.dll")]

The case of the difficult constructor arguments

If the problem is that the constructor requires too many arguments then you should ask yourself if this class is too big, maybe it has too many responsibilities and don't follow the Single Responsibility Principle? In that case the class should be splitted into two or more classes. This way it will be easier to test the smaller class because its constructor should require less arguments.

Another case is when the arguments are difficult to create or provide. Maybe that in order to create one object to pass as parameter we need to create yet more objects. Micheal Feathers calls it the Onion Parameter in his book Working Effectively with Legacy Code. In that case I usually invest into a bit of testing infrastructure like factory methods or Test Data Builder in order to help me write tests faster afterwards.

Finally some parameters could be classes that wrap services or external dependencies. In that case I abstract the dependency using an interface and the Inversion of Control Principle. First extract an interface from the class. Next change the constructor to use this interface instead of the class. Finally use a mocking framework like Moq to create a fake (or create your own by hand if you don't like mocking frameworks) and pass it to the constructor.

public class MyService
{
    // public MyService(SomeDataAccessLayer dal, SomeExternalService externalService)
    public MyService(ISomeDataAccessLayer dal, ISomeExternalService externalService)
    {
        // ...
    }

    // ...
}

The case of the external dependencies calls

When the constructor itself calls an external dependency I usually refactor the constructor to inject this dependency via a parameter instead, this is called dependency injection and usually comes with the Inversion of Control Principle. This is also a good occasion to take a look at a IoC Container like Unity.

public class MyService
{
    // public MyService()
    public MyService(ISomeExternalService externalService)
    {
        // ...
        // var externalService = new ExternalService();
        externalService.CallService();
    }

    // ...
}

Saturday, September 6, 2014

Unit test smells: The non-public class

Writing unit tests for a method on a class that is not public is doable but not straight forward. It could be done using a bit of reflection like this

var type = Type.GetType("MyProject.MyClass");
var methodInfo = type.GetMethod("TheMethod");

var classInstance = Activator.CreateInstance(type, null);
methodInfo.Invoke(classInstance, null);

That is quite a bit of work. Of course you could create some test infrastructure to avoid duplication in every tests or you may find libraries online for that. But still, writing tests like this smells funny to me.

In short, you should never write tests directly against a non-public class.
Let's take a look at the 2 possible cases for this: the private class and the internal class.

The case of the private class

For a class to be private means it is nested inside another class

public class A
{
    // ...

    private class B
    {
        // ...
    }
}

In a situation like this class B can only be used by class A. Anything class B do is only for serving class A. If you want to test a method in class B you should find out how class A use class B and write your tests against class A public API.

If you find there is no way to reach the method you want to test in class B through class A it simply means that you just found dead code! If you found a way but find it too hard to setup a test then maybe class B is not simply a private utility class. In that case I would extract class B from inside class A and change its access modifier to internal. Unfortunately, now any class inside the same assembly could use class B. It is a trade-off I'm willing to pay because this case is really rare and C# still lack a proper access modifier scoped to the current namespace.

The case of the internal class

Internal class could also be tested through reflection. Personally I prefer to use the InternalsVisibleToAttribute and give access to internals types to my unit test project. To do it you need to add the attribute to the project under test AssemblyInfo.cs file like this

[assembly: InternalsVisibleTo("Contoso.MyApp.UnitTests.dll")]

If your project is signed you will need to also provide the assembly's public key like this

[assembly: InternalsVisibleTo("Contoso.MyApp.UnitTests.dll, PublicKey=1234...789")]

Again, this is a trade-off: you give special access to your internal types only for testing but it is way better than changing the class access modifier to public. You should never change a type access modifier to public for testing!

Conclusion

I always try to write tests against public classes and methods. Sometime an internal class will grow to be very complex over time and become its own new component. In a solution were we create a lot of small projects we would simply create a new one for such component and expose it publicly but that is not what I do. I usually try to create the minimal number of projects in my solution so testing internal classes using the InternalsVisibleToAttribute is a good trade-off for me.

Sunday, July 13, 2014

Exploring BDDfy

BDDfy is a BDD library (part of the larger TestStack project). It's function is to turn regular unit tests to BDD style tests (using the Gherkin syntax). You can read more about BDDfy here.

BDDfy can be used with any test framework or runner.

Acquiring

To add BDDfy to your test project via NuGet run this command in the Package Manage Console.

Install-Package TestStack.BDDfy

Optionally, you can also install code samples with this package.

Install-Package TestStack.BDDfy.Samples

Exploring


Hello world

For this part I'll be using the xUnit test framework. Let's start with something simple.

public class FirstTest
{
    void GivenTwoAndTwo()
    {
        // ...
    }

    void WhenIAddThem()
    {
        // ...
    }

    void ThenTheAnwserShouldBe4()
    {
        // ...
    }

    [Fact]
    public void ExecuteFirstTest()
    {
        this.BDDfy();
    }
}

All the magic is done by the BDDFy extension methods. This will scan the FirstTest class for methods starting with keywords like Given, When and Then. Next, it will run the methods in order (BDD style). Finally, we will get a nice output report like this.

Test output in Visual Studio

BDDfy will also generate a BDDfy.html file in the test project output folder. This is the report of all BDDFyed tests.

HTML tests report

Using attributes to customize the test

BDDfy follow conventions when scanning a class for methods of interest, you can find a list here. If we need more control we can do it by using attributes.

[Story(
    AsA = "As someone lazy",
    IWant = "I want the computer to add 2 number",
    SoThat = "I don't have to do the math myself")]
public class TestWithAttributesToOverriteText
{
    [Given("Given 2 + 2")]
    void GivenTwoAndTwo()
    {
        // ...
    }

    [When(" + ")]
    void WhenIAddThem()
    {
        // ...
    }

    [Then("Then the anwser = 4")]
    void ThenTheAnwserShouldBe4()
    {
        // ...
    }

    void AndThenItShouldDisplayTheAnwser()
    {
    }

    [Fact]
    public void ExecuteTestWithAttributes()
    {
        this.BDDfy();
    }
}

First, the [Story] attribute allow us to provide the classic story definition for the test. Other attributes like [Given], [When] and [Then] allow us to provide a custom description for the steps. Also, using the attributes will allow us to name the step methods the way we want.

Creating more than one scenario per story

Usually a story contains more than one test or scenario. We can do this using nested classes.

[Story(
    Title = "Using story attribute and setting the Title!",
    AsA = "As someone learning BDDfy",
    IWant = "I want to try splitting scenario in separated classes",
    SoThat = "My code is cleaner")]
public class TestWithStoryAndScenarioInSeparatedClasses
{
    [Fact]
    public void FirstScenario()
    {
        new S1().BDDfy("Custom scenario title");
    }

    [Fact]
    public void SecondScenario()
    {
        new S2().BDDfy();
    }

    private class S1
    {
        void GivenWhatever() { // ... }
        void WhenSomethingHappens() { // ... }
        void ThenProfit() { // ... }
    }
    private class S2
    {
        void GivenWhatever() { // ... }
        void WhenSomethingElseHappens() { // ... }
        void ThenProfit() { // ... }
    }
}

This will group scenarios together in the output report.

Using the fluent API for even more control

With what we've seen previously we need to create a new test class for each scenario we have. That leads to a lot of duplicated code unless we delegate to a common test fixture. An alternative is to use BDDfy fluent API to get some code reuse between our scenarios.

public class TestWithFluentApi
{
    [Fact]
    public void ReusingStepForScenario1()
    {
        new TestWithFluentApi()
            .Given(s => s.GivenWhatever(), "Given some pre-condition")
                .And(s => s.AndOtherGiven(54))
            .When(s => s.WhenSomethingElseHappens())
            .Then(s => s.ThenProfit())
                .And(s => s.AndManyMore(45))
            .BDDfy();
    }

    [Fact]
    public void ReusingStepForScenario2()
    {
        new TestWithFluentApi()
            .Given(s => s.GivenWhatever(), "Given some pre-condition")
                .And(s => s.AndOtherGiven(123))
            .When(s => s.WhenSomethingElseHappens())
            .Then(s => s.ThenProfit())
                .And(s => s.AndManyMore(321), "And {0} more things!")
            .BDDfy("Scenario 2 with steps re-use");
    }

    void GivenWhatever() { // ... }
    void AndOtherGiven(int input) { // ... }
    void WhenSomethingElseHappens() { // ... }
    void ThenProfit() { // ... }
    void AndManyMore(int expected) { // ... }
}

With this style of test, not only we can reuse steps between scenarios we also gain the ability to parameterize the steps.

Assessment

I've only scratched the surface of what BDDfy can do. You can read more about on BDDfy usage and customization on the project web site.

In the past I've use SpecFlow for my BDD tests. With SpecFlow you write your specification in a text file using the Gherkin language. The tool then parse the file and execute corresponding method for each steps. Having a text file seems interesting because we could have a business analyst write those. In really, developers ends up writing the stories and scenarios anyway.

This is why I like BDDfy, it's easy to learn and gives developers a lot of control over the way we create BDD style tests.

One more for my toolbox!

I hope you enjoyed this introduction to BDDfy.

Friday, August 31, 2012

Using log4net MemoryAppender for unit testing

Sometimes unit testing existing code can be really hard. Especially when coding by exceptions (using Try.. Catch.. then do nothing!).

This will look a bit like this



From the outside we might not be able to test whether ReallyDoSomething has thrown an exception except if we can validate state changes done by ReallyDoSomething. And if we can't change the code much (like adding dependencies using parameters or change the return type of the method) we won't be able to write that test at all.

Another way is to use the logger to validate that.

The piece of code above use Log4Net as the logging framework and allow us to use it in our unit tests like this



Just like that we can intercept all the logged messages by our production code.

I've used this trick a few times now when other options were not available. Hope it helps.

Thursday, November 25, 2010

Pex4Fun

Check out this really cool web site. Not only you can write code in your browser you also have IntelliSense. The way it works is you are given a puzzle or a duel to solve (think code breaker C# edition). The goal of the game is to write the same algorithm than the hidden solution. You get hints by running Pex against both the hidden puzzle and your solution.

Pex is a tool that will try to call a function with any kind of input parameters required to use all the code path of the function. It will highlight thing like passing null might cause a NullReferenceException down the line and you should guard against it. So by trying all those combinations against your code and the hidden puzzle will give hints on the difference of implementation. You can then update your code and try again each time getting closer and closer to the solution.

You can even create an account and keep track of all the challenges you solved. A section of the site is structured as a course where you learn about C# and cool features like Code Contracts.

Code Contracts helps us create a more declarative way of expressing pre and post conditions to our code. And because of the declarative way they works, tools can then to provides us with static compile time validation, run time guard code and automated API documentation of the contracts.

Pex4Fun is really a great way to play around those 2 great tools without having to setup libraries on our machine. I can even show some basic C# stuff to my friends, on the web and even from my smart phone (but then it’s not that fun I must admit!)

Tuesday, November 17, 2009

Executable specifications podcast

I participated in a Visual Studio Talk Show podcast recording last week.

It is available now here (only in French sorry).

You can read more on the subject on the excellent site of Scott W. Ambler: Agile modeling.