How to structure my unit test?

IamaC

I am new to unit test and wondering how to start testing. The application I am currently working on, does not have any unit test. It a winform application and I am only interested to test the data layer of this application.

Here is an example.

public interface ICalculateSomething
{
     SomeOutout1 CalculateSomething1(SomeInput1 input1);
     SomeOutout2 CalculateOSomething2(SomeInput2 input2);
}

public class CalculateSomething : ICalculateSomething
{
    SomeOutout1 ICalculateSomething.CalculateSomething1(SomeInput1 input1)
    {
        SomeOutout1.Prop1 = calculateFromInput1(input1.Prop1, input1.Prop2);

        SomeOutout1.Prop3 = calculateFromInput2(input1.Prop3, input1.Prop4);

        return SomeOutout1;
    }

    SomeOutout2 ICalculateSomething.CalculateOSomething2(SomeInput2 input2)
    {
        SomeOutout2.Prop1 = calculateFromInput1(input2.Prop1, input2.Prop2);

        SomeOutout2.Prop3 = calculateFromInput2(input2.Prop3, input2.Prop4);

        return SomeOutout2;
    }
}

I would like to test these two methods in the CalculateSomething. Those methods implementation are long and complicated. How should I structure my test?

Heinzi

I don't see a reason for not using a straight-forward unit test implementation. I'd start with a basic test method:

[TestMethod]
public void CalculateSomething1_FooInput
{
    var input = new SomeInput1("Foo");
    var expected = new SomeOutput1(...);

    var calc = new CalculateSomething(...);
    var actual = calc.CalculateSomething1(input);

    Assert.AreEqual(expected.Prop1, actual.Prop1);
    Assert.AreEqual(expected.Prop2, actual.Prop2);
    Assert.AreEqual(expected.Prop3, actual.Prop3);
}

And then, as you add CalculateSomething1_BarInput and CalculateSomething2_FooInput, factor out some common code into helper methods:

[TestMethod]
public void CalculateSomething1_FooInput
{
    var input = new SomeInput1("Foo");
    var expected = new SomeOutput1(...);

    var actual = CreateTestCalculateSomething().CalculateSomething1(input);

    AssertSomeOutput1Equality(expected, actual);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to fix the unit test issue in my case?

From Dev

How to unit test the catch body of my Observable

From Dev

How to structure my angularjs app and test folder

From Dev

How do I unit test and integration test my SSIS packages?

From Dev

How to solve my unit test issue in my case

From Dev

How to unit test my HtmlUnit code with an xml file

From Dev

How can I tell that my program is under unit test environment

From Dev

how can I write unit test to my meteor methods?

From Dev

how can I control the browser agent in my wicket unit test

From Dev

Not getting how to write unit test function of my function

From Dev

How do i Unit Test my RelayCommand using Moq?

From Dev

How can I unit test for a stack overflow in my factorial function?

From Dev

How to structure my RSpec test folders, files and database?

From Dev

AngularJS unit test directory structure Best Practices

From Dev

Is there a way to generate unit test to test my grammar

From Dev

How to unit test FileContentResult?

From Dev

How to unit test DataContractSerializer?

From Dev

How to unit test if "." is not on @INC?

From Dev

How to unit test FileContentResult?

From Dev

How does Robolectric work? Why does my unit test download some jars while first run Unit Test?

From Dev

How can I structure a mocha unit test that involves promises and third party NPM modules?

From Dev

How do I do a jasmine unit test for my angular directive that needs to test its emit?

From Dev

How do I mock controller context in my unit test so that my partial view to string function works?

From Dev

How do I mock controller context in my unit test so that my partial view to string function works?

From Dev

How do I structure my Android Studio project to avoid compilation issues with running the app with unit tests integrated

From Dev

Why is my jasmine unit test not waiting for 'done'?

From Dev

Jasmine unit test skipped by my Karma configuration

From Dev

Cannot unit test my model in sailsjs

From Dev

Automake config cannot compile my unit test

Related Related

  1. 1

    How to fix the unit test issue in my case?

  2. 2

    How to unit test the catch body of my Observable

  3. 3

    How to structure my angularjs app and test folder

  4. 4

    How do I unit test and integration test my SSIS packages?

  5. 5

    How to solve my unit test issue in my case

  6. 6

    How to unit test my HtmlUnit code with an xml file

  7. 7

    How can I tell that my program is under unit test environment

  8. 8

    how can I write unit test to my meteor methods?

  9. 9

    how can I control the browser agent in my wicket unit test

  10. 10

    Not getting how to write unit test function of my function

  11. 11

    How do i Unit Test my RelayCommand using Moq?

  12. 12

    How can I unit test for a stack overflow in my factorial function?

  13. 13

    How to structure my RSpec test folders, files and database?

  14. 14

    AngularJS unit test directory structure Best Practices

  15. 15

    Is there a way to generate unit test to test my grammar

  16. 16

    How to unit test FileContentResult?

  17. 17

    How to unit test DataContractSerializer?

  18. 18

    How to unit test if "." is not on @INC?

  19. 19

    How to unit test FileContentResult?

  20. 20

    How does Robolectric work? Why does my unit test download some jars while first run Unit Test?

  21. 21

    How can I structure a mocha unit test that involves promises and third party NPM modules?

  22. 22

    How do I do a jasmine unit test for my angular directive that needs to test its emit?

  23. 23

    How do I mock controller context in my unit test so that my partial view to string function works?

  24. 24

    How do I mock controller context in my unit test so that my partial view to string function works?

  25. 25

    How do I structure my Android Studio project to avoid compilation issues with running the app with unit tests integrated

  26. 26

    Why is my jasmine unit test not waiting for 'done'?

  27. 27

    Jasmine unit test skipped by my Karma configuration

  28. 28

    Cannot unit test my model in sailsjs

  29. 29

    Automake config cannot compile my unit test

HotTag

Archive