Click here to Skip to main content
15,881,600 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I've just been notified I'm rich :) Pin
DerekT-P28-Jan-22 8:46
professionalDerekT-P28-Jan-22 8:46 
GeneralRe: I've just been notified I'm rich :) Pin
charlieg28-Jan-22 8:51
charlieg28-Jan-22 8:51 
QuestionHow much you feel happy after solve problem of anyone? Pin
M Imran Ansari27-Jan-22 11:09
mveM Imran Ansari27-Jan-22 11:09 
AnswerRe: How much you feel happy after solve problem of anyone? Pin
PIEBALDconsult27-Jan-22 11:20
mvePIEBALDconsult27-Jan-22 11:20 
AnswerRe: How much you feel happy after solve problem of anyone? Pin
Slow Eddie27-Jan-22 11:24
professionalSlow Eddie27-Jan-22 11:24 
AnswerRe: How much you feel happy after solve problem of anyone? Pin
RickZeeland27-Jan-22 19:13
mveRickZeeland27-Jan-22 19:13 
GeneralRe: How much you feel happy after solve problem of anyone? Pin
honey the codewitch28-Jan-22 0:03
mvahoney the codewitch28-Jan-22 0:03 
QuestionI'm fairly old fashioned at times ... but should I embrace unit testing? Pin
OriginalGriff27-Jan-22 5:57
mveOriginalGriff27-Jan-22 5:57 
At the moment, when I create a public class called Foo, I also create a static test class called FooTester which exercises the public members of the class as best I can.
For example, my non-contiguous range class Range has a RangeTest class that looks like this:
C#
public static string Run()
    {
    try
        {
        testsRun = 0;
        testsPassed = 0;
        testsFailed = 0;
        outText.Clear();
        Range ra = new Range(0, 4);       //
        Range rb = new Range(5, 9);       // Contiguous with ra
        Range rc = new Range(10, 14);     // Contiguous with rb
        Range rd = new Range(1, 3);       // Intersects ra, not contiguous with rb
        Range re = new Range(6, 8);       // Intersects rb, not contiguous with rc
        Range rf = new Range(16, 19);     // Not contiguous with anything.
        Range rg = new Range(0, 0);       // Single element.
        Range rh = new Range(1, 1);       // Single element.
        Range ri = new Range(2, 2);       // Single element.
        Range rj = new Range(0, 19);      // All elements.
        Range rk = new Range(0, 4);       // Identical to ra

        CheckInt(ra.Count, 5);
        CheckInt(ra.Min, 0);
        CheckInt(ra.Max, 4);
        CheckString(ra, "(0:4)");
        CheckString(ra.UnionWith(re), "(0:4) | (6:8)");

        Check(Range.GetEmpty(), new int[0]);

        Check(ra, new int[] { 0, 1, 2, 3, 4, });
        Check(new Range(new int[] { 0, 1, 2, 5, 6, 7 }.AsEnumerable()), new int[] { 0, 1, 2, 5, 6, 7 });
        Check(ra.UnionWith(ra), new int[] { 0, 1, 2, 3, 4 });
        Check(ra.UnionWith(rb), new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
        Check(rb.UnionWith(rc), new int[] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 });
        Check(ra.UnionWith(rc), new int[] { 0, 1, 2, 3, 4, 10, 11, 12, 13, 14 });
        Check(ra.UnionWith(re), new int[] { 0, 1, 2, 3, 4, 6, 7, 8 });
        Check(ra.UnionWith(rg), new int[] { 0, 1, 2, 3, 4 });
        Check(rb.UnionWith(rg), new int[] { 0, 5, 6, 7, 8, 9 });
        Check(rg.UnionWith(rh).UnionWith(ri), new int[] { 0, 1, 2 });
        CheckInt(ra.UnionWith(rc).Count, 10);
        CheckInt(ra.UnionWith(rc).Min, 0);
        CheckInt(ra.UnionWith(rc).Max, 14);

        Check(ra.Intersecting(ra), new int[] { 0, 1, 2, 3, 4 });
        Check(ra.Intersecting(rb), new int[0]);
        Check(rb.Intersecting(rc), new int[0]);
        Check(ra.Intersecting(rc), new int[0]);
        Check(ra.Intersecting(rg), new int[] { 0 });
        Check(rb.Intersecting(rg), new int[0]);
        Check(rg.Intersecting(rh).Intersecting(ri), new int[0]);

        Check(ra.Excepting(ra), new int[0]);
        Check(ra.Excepting(rb), new int[] { 0, 1, 2, 3, 4 });
        Check(rb.Excepting(rc), new int[] { 5, 6, 7, 8, 9 });
        Check(ra.Excepting(rc), new int[] { 0, 1, 2, 3, 4 });
        Check(ra.Excepting(rg), new int[] { 1, 2, 3, 4 });
        Check(rj.Excepting(rh).Excepting(rb).Excepting(rf), new int[] { 0, 2, 3, 4, 10, 11, 12, 13, 14, 15 });
        Check(rg.Excepting(rh).Excepting(ri), new int[] { 0 });

        Check(ra.EndAt(rj.Max), new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 });
        Check(rf.StartAt(ra.Min), new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 });
        Check(ra.UnionWith(re).MakeContiguous(), new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });

        Check(ra.Add(5), new int[] { 0, 1, 2, 3, 4, 5 });
        Check(ra.Add(-1), new int[] { -1, 0, 1, 2, 3, 4 });
        Check(ra.AddRange(new int[] { 5, -1 }), new int[] { -1, 0, 1, 2, 3, 4, 5 });
        Check(ra + 5, new int[] { 0, 1, 2, 3, 4, 5 });
        Check(ra + -1, new int[] { -1, 0, 1, 2, 3, 4 });
        Check(ra + new int[] { 5, -1 }, new int[] { -1, 0, 1, 2, 3, 4, 5 });
        Check(ra + rb, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

        Check(ra.Remove(1), new int[] { 0, 2, 3, 4 });
        Check(ra.RemoveRange(new int[] { 1, 2 }), new int[] { 0, 3, 4 });
        Check(ra - 1, new int[] { 0, 2, 3, 4 });
        Check(ra - -1, new int[] { 0, 1, 2, 3, 4 });
        Check(ra - new int[] { 1, 2 }, new int[] { 0, 3, 4 });
        Check(ra - rd, new int[] { 0, 4 });

        CheckBool(ra.Equals(rk), true);
        CheckBool(ra.Equals((object)rk), true);
        CheckBool(ra == rk, true);
        CheckBool(ra != rk, false);

        CheckBool(ra.IsContiguousWith(rb), true);
        CheckBool(ra.IsContiguousWith(rc), false);
        CheckBool(ra.IsContiguousWith(rd), false);
        CheckBool(ra.IsIntersecting(rb), false);
        CheckBool(ra.IsIntersecting(rc), false);
        CheckBool(ra.IsIntersecting(rd), true);

        }
    catch (Exception ex)
        {
        outText.AppendLine($"Exception in testing: \"{ex.Message}\"");
        }
    outText.AppendLine($"Tests: {testsRun}: Passed: {testsPassed} Failed: {testsFailed}");
    return outText.ToString();
    }
along with the service methods it needs to check and report:
C#
/// <summary>
/// Check a boolean result
/// </summary>
/// <param name="result"></param>
/// <param name="expected"></param>
private static void CheckBool(bool result, bool expected)
    {
    testsRun++;
    if (result == expected)
        {
        testsPassed++;
        return;
        }
    ReportFailure("Logical test failed", result.ToString(), expected.ToString());
    }
/// <summary>
/// Check a integer result
/// </summary>
/// <param name="result"></param>
/// <param name="expected"></param>
private static void CheckInt(int result, int expected)
    {
    testsRun++;
    if (result == expected)
        {
        testsPassed++;
        return;
        }
    ReportFailure("Integer comparison failed", result.ToString(), expected.ToString());
    }
/// <summary>
/// Check a string result
/// </summary>
/// <param name="result"></param>
/// <param name="expected"></param>
private static void CheckString(Range range, string expected)
    {
    testsRun++;
    string strRange = range.ToString();
    if (strRange == expected)
        {
        testsPassed++;
        return;
        }
    ReportFailure("Human readable data did not match", strRange, expected);
    }
/// <summary>
/// Check a Range result
/// </summary>
/// <param name="result"></param>
/// <param name="expected"></param>
private static void Check(Range range, int[] expected)
    {
    testsRun++;
    string result = "Range length incorrect";
    if (range.Count == expected.Length)
        {
        result = "Range values did not match";
        if (Enumerable.SequenceEqual(range, expected))
            {
            testsPassed++;
            return;
            }
        }
    ReportFailure(result, range, expected);
    }
/// <summary>
/// Report a failure
/// </summary>
/// <param name="result"></param>
/// <param name="range"></param>
/// <param name="expected"></param>
private static void ReportFailure(string result, string range, string expected)
    {
    testsFailed++;
    outText.AppendLine($"Test #{testsRun}: {result} ");
    outText.AppendLine($"   Got     : {range}");
    outText.AppendLine($"   Expected: {expected}");
    }
/// <summary>
/// Report a failure
/// </summary>
/// <param name="result"></param>
/// <param name="range"></param>
/// <param name="expected"></param>
private static void ReportFailure(string result, Range range, int[] expected)
    {
    testsFailed++;
    outText.AppendLine($"Test #{testsRun}: {result} ");
    outText.AppendLine($"   Got     : {string.Join(", ", range.Select(r => r.ToString()))}");
    outText.AppendLine($"   Expected: {string.Join(", ", expected.Select(r => r.ToString()))}");
    }
}
And it works: I throw every possibility I can think of in there together with the expected result, and I get a message saying it passed or where it failed.

I've been doing this for decades - it allows me to run tests to show a bug exists, and that it's fixed without breaking any other tests.

But as I said, I'm old fashioned sometimes - and along came Unit Testing and I ignored it completely. But should I have? Or should I think of biting bullets and embracing the change?

Anyone using it? Using something like what I do? Or are you all philistines who don't automate tests at all? Laugh | :laugh:
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!

AnswerRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
RickZeeland27-Jan-22 6:11
mveRickZeeland27-Jan-22 6:11 
GeneralRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
Marc Clifton27-Jan-22 7:50
mvaMarc Clifton27-Jan-22 7:50 
GeneralRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
Davyd McColl27-Jan-22 21:44
Davyd McColl27-Jan-22 21:44 
GeneralRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
RickZeeland27-Jan-22 21:52
mveRickZeeland27-Jan-22 21:52 
GeneralRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
Davyd McColl27-Jan-22 22:32
Davyd McColl27-Jan-22 22:32 
GeneralRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
RickZeeland27-Jan-22 22:57
mveRickZeeland27-Jan-22 22:57 
GeneralRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
Davyd McColl27-Jan-22 23:05
Davyd McColl27-Jan-22 23:05 
AnswerRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
PIEBALDconsult27-Jan-22 6:20
mvePIEBALDconsult27-Jan-22 6:20 
AnswerRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
Mircea Neacsu27-Jan-22 6:34
Mircea Neacsu27-Jan-22 6:34 
GeneralRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
Marc Clifton27-Jan-22 7:51
mvaMarc Clifton27-Jan-22 7:51 
GeneralRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
Mircea Neacsu27-Jan-22 8:01
Mircea Neacsu27-Jan-22 8:01 
AnswerRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
Duncan Edwards Jones27-Jan-22 6:39
professionalDuncan Edwards Jones27-Jan-22 6:39 
AnswerRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
pkfox27-Jan-22 6:43
professionalpkfox27-Jan-22 6:43 
AnswerRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
Gerry Schmitz27-Jan-22 7:02
mveGerry Schmitz27-Jan-22 7:02 
AnswerRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
kmoorevs27-Jan-22 7:02
kmoorevs27-Jan-22 7:02 
AnswerRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
Greg Utas27-Jan-22 7:07
professionalGreg Utas27-Jan-22 7:07 
AnswerRe: I'm fairly old fashioned at times ... but should I embrace unit testing? Pin
theoldfool27-Jan-22 7:09
professionaltheoldfool27-Jan-22 7:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.