How to define test for method>

loluros

New member
Joined
Jan 29, 2022
Messages
4
Programming Experience
Beginner
How to write unit tests for testing the KwHpConverter method. It is necessary to define a larger number of test cases and try to find a test case that will end in failure. Errors that you detect in the testing process need to be corrected.
The code to test is shown down:
C#:
namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {

            double result = 0;

            Console.WriteLine("Please, enter value: ");
            double value = double.Parse(Console.ReadLine());

            Console.WriteLine("Convert to (enter kw or hp(sad)");
            string convertTo = Console.ReadLine();

            if(convertTo == "kw")
            {
                result = KwHpConverter(value, "kw");
            } else
            {
                result = KwHpConverter(value, "hp");
            }

            Console.WriteLine(result);

        }

        private static double KwHpConverter (double inputValue, string convertTo)
        {
            if(convertTo == "hp")
            {
                return inputValue / 0.745699872;
            } else
            {
                return inputValue * 0.745699872;
            }
        }

    }

}
 
Last edited by a moderator:
Why is it necessary to define a large number of test cases? You can make if fail simply by passing null as the second parameter:
C#:
KwHpConverter(1, null);

Other ways to make it fail is by causing the multiplication or division to overflow.
 
It is necessary to define a larger number of test cases and try to find a test case that will end in failure. Errors that you detect in the testing process need to be corrected.
Those two sentences sound like part of a homework assignment that you have copied and pasted without telling us that it's a homework assignment. Is that the case?

Unit tests are basically just calls to that method and then checks that the end state is what's expected. If you want to write proper unit tests then you should add a proper test project to your solution, but we don't know what the actual expectations are.
 
Learn how to write proper unit test, see the following for the basics.

Then look at best practices for writing unit test.

Writing unit test is a completely different mindset than writing application code and with that one new to writing unit test should not just dive in but instead take time to understand the basics and best practices. A professional will use software to write out scenarios for testing followed by writing out stubs test methods for each scenario, examine what has been stubbed out and decide, is this going to cover all possible usages of what is being test, if good then start writing test method, if not add more stubbed test methods. As you become more familiar with writing test there are things you will learn to become more proficient writing test.

Once understood how to create unit test, write down on paper, in a word process or other software what to test then write out shell test methods. If there are business customers, ask for their input for the intended usage of your code and not the code itself.

Stub test method for when the first parameter is not in an acceptable range.

Stub test method:
[TestMethod]
public void KwHpConverter_FirstParameter_RangeValidation()
{
    // TODO  ArgumentOutOfRangeException
}

While writing test methods you may find out there are appropriate assertions for your code. For example, assert, in this case the second parameter can not be null.

Assert convertTo is not null:
if (string.IsNullOrWhiteSpace(convertTo))
{
    throw new ArgumentNullException($"{nameof(convertTo)} can not be null");
}

Then what if for the second parameter the value is HP? Consider using string.Equals and consider other information passed in.

string case insensitive logic:
return string.Equals(convertTo, "hp", StringComparison.CurrentCultureIgnoreCase) ?
    inputValue / 0.745699872 :
    inputValue * 0.745699872;

While writing test, write methods to validate positive and negative results e.g.

writing for excepted exceptions:
[TestMethod]
[ExpectedException(typeof(ArgumentNullException), "TODO")]
public void KwHpConverter_PassingNull_ForConvertToParameter()
{
    Helpers.KwHpConverter(12, null);
}

[TestMethod]
public void KwHpConverter_FirstParameterInput_WithAcceptableValue()
{
    var expected = 16.09226506612569;
    Assert.AreEqual(Helpers.KwHpConverter(12,"hp"), expected);
}
 
Great write up, but recall that doing equality checks with floating point numbers will require extra care and attention. Simply using AreEqual() often won't cut it.
 
Great write up, but recall that doing equality checks with floating point numbers will require extra care and attention. Simply using AreEqual() often won't cut it.
Agree with floating points and would take a different approach.
 
Why is it necessary to define a large number of test cases? You can make if fail simply by passing null as the second parameter:
C#:
KwHpConverter(1, null);

Other ways to make it fail is by causing the multiplication or division to overflow.
Thank you for your answer
 
Those two sentences sound like part of a homework assignment that you have copied and pasted without telling us that it's a homework assignment. Is that the case?

Unit tests are basically just calls to that method and then checks that the end state is what's expected. If you want to write proper unit tests then you should add a proper test project to your solution, but we don't know what the actual expectations are.
Yes its homework. Thats all I have, but thanks for your answer very much
 
Learn how to write proper unit test, see the following for the basics.

Then look at best practices for writing unit test.

Writing unit test is a completely different mindset than writing application code and with that one new to writing unit test should not just dive in but instead take time to understand the basics and best practices. A professional will use software to write out scenarios for testing followed by writing out stubs test methods for each scenario, examine what has been stubbed out and decide, is this going to cover all possible usages of what is being test, if good then start writing test method, if not add more stubbed test methods. As you become more familiar with writing test there are things you will learn to become more proficient writing test.

Once understood how to create unit test, write down on paper, in a word process or other software what to test then write out shell test methods. If there are business customers, ask for their input for the intended usage of your code and not the code itself.

Stub test method for when the first parameter is not in an acceptable range.

Stub test method:
[TestMethod]
public void KwHpConverter_FirstParameter_RangeValidation()
{
    // TODO  ArgumentOutOfRangeException
}

While writing test methods you may find out there are appropriate assertions for your code. For example, assert, in this case the second parameter can not be null.

Assert convertTo is not null:
if (string.IsNullOrWhiteSpace(convertTo))
{
    throw new ArgumentNullException($"{nameof(convertTo)} can not be null");
}

Then what if for the second parameter the value is HP? Consider using string.Equals and consider other information passed in.

string case insensitive logic:
return string.Equals(convertTo, "hp", StringComparison.CurrentCultureIgnoreCase) ?
    inputValue / 0.745699872 :
    inputValue * 0.745699872;

While writing test, write methods to validate positive and negative results e.g.

writing for excepted exceptions:
[TestMethod]
[ExpectedException(typeof(ArgumentNullException), "TODO")]
public void KwHpConverter_PassingNull_ForConvertToParameter()
{
    Helpers.KwHpConverter(12, null);
}

[TestMethod]
public void KwHpConverter_FirstParameterInput_WithAcceptableValue()
{
    var expected = 16.09226506612569;
    Assert.AreEqual(Helpers.KwHpConverter(12,"hp"), expected);
}
Dear Karen your answer is so usefull, Iv done it. Thanks so much
 
Back
Top Bottom