locked
Debug and Test output functions. RRS feed

  • Question

  • User387004 posted

    I've not done unit testing in code in a long time. So I no experience with the Google or Mono test set functions. I've created my own set of test output functions, but these functions use the compiler services class for giving me callers, lines, and so on. Is there a way to specify DEBUG reigons that would exclude the inclusion of these libs from my production code? In the past I could simply surround the code in the functions in a #IFDEF __DEBUG directive, but since the CompilerServices functions are called in the function prototype I think they are going to be included since those are a part of the compiled code. Even if I exclude all of the internal code from the function with a compiler directive like before with the #IFDEF __DEBUG the function prototype would still be compiled and include those libraries.

    My concern has to do with if these libs are included will they create an overhead in size or speed of code, also would this expose code to an easier decompile or Function/Variable snooping?

    Thanks! Sorry if this is an asinine question. Cheers!

    Friday, January 22, 2021 11:19 PM

Answers

  • User387683 posted

    In CSharp, you could use DebuggerStepThrough to skip when debug.

       private void Button_Clicked(object sender, EventArgs e)
        {
            method();
        }
    
        [DebuggerStepThrough]       
        public void method()
        {
            Console.WriteLine("hello world");//this would not be triggled when debug
        }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, January 27, 2021 8:44 AM

All replies

  • User387683 posted

    Is there a way to specify DEBUG reigons that would exclude the inclusion of these libs from my production code? What does this mean? It seems you wanna to use the UITest for the ios project. Do you want to avoid loading some specific libraries? Does these libraries be referenced with the code you want to test? If yes, it could not do that.

    Monday, January 25, 2021 8:56 AM
  • User387004 posted

    Sorry if my post was confusing. Here's a break down of what I am talking about Let's say I am using a lib to create some debug output, This lib adds a decent bit to the built binary, and adds a lot of code that I don't want included in my production build. In the past when I was a C++ programmer, I could define regions as #ifdef _DEBUG_ this code would be ignored by the compiler when I compile the release binary. That way my production code wouldn't include libraries or code that could be used to compromise other parts of the code or expose sensitive data. I'm not entirely familiar with Xamarin, or C#, so I am not sure if I were to use compiler directives if it would indeed strip the libraries entirely if they are included in the project, but not used.

    IE This code uses some external libraries that I created to validate data being passed, also includes libs to decrypt portions of secure data storage. I don't want this code to be included in the production build. It also includes CompilerServices which I would prefer to not have in the production build.

    public var SomeFunction (MyDataType variable)
    {
        .... some code that populates the variable ....
    
        if (TestTools.Log(variable))
        {
            ...Perform some perfunctory function to debug the value...
        }
    }
    
        public static class TestTools
    {
        public static bool Log(Object testvar,
                            string text = "Variable Test",
                            [CallerFilePath] string file = "",
                            [CallerMemberName] string member = "",
                            [CallerLineNumber] int line = 0)
                {
    
                    if (testvar== null)
                {
                            Debug.WriteLine("{0}->{1}({2}): {3} --> {4} is NULL ",
                                    file, member, line, text, testvar.GetType().ToString());
                    return false;
                }
                Debug.WriteLine("{0}->{1}({2}): {3} --> {4} Not Null ",
                                    file, member, line, text, testvar.GetType().ToString());
    
                var = TestValidationFunction(testvar);
    
                return var;
                 }
        }
    

    in the past I could use a #ifdef _DEBUG_ around the code

    #ifdef _DEBUG_
        if (TestTools.Log(variable))
        {
            ...Perform some perfunctory function to debug the value...
        }
    #endif
    

    and then the same around the

       #ifdef _DEBUG_
    #Using MyTestingLibrary
    
    public static class TestTools
    {
        public static bool Log(Object testvar,
                            string text = "Variable Test",
                            [CallerFilePath] string file = "",
                            [CallerMemberName] string member = "",
                            [CallerLineNumber] int line = 0)
            {
    
                if (testvar== null)
            {
                        Debug.WriteLine("{0}->{1}({2}): {3} --> {4} is NULL ",
                                file, member, line, text, testvar.GetType().ToString());
                return false;
            }
            Debug.WriteLine("{0}->{1}({2}): {3} --> {4} Not Null ",
                                file, member, line, text, testvar.GetType().ToString());
    
            var = TestValidationFunction(testvar);
    
            return var;
             }
    }
    #endif
    

    In the end, this would cause the compiler to ignore all of the debug code, and since the included libraries had no calls, they would be excluded from the binary.

    As far as I know that's what happens with C#, but I'm not certain of that. I should do my unit testing at a finer level, so I can just use debug outputs on individual structures, but that would require a considerable bit of coding.

    Hope this explains it better.

    Cheers!

    Tuesday, January 26, 2021 12:39 AM
  • User387683 posted

    In CSharp, you could use DebuggerStepThrough to skip when debug.

       private void Button_Clicked(object sender, EventArgs e)
        {
            method();
        }
    
        [DebuggerStepThrough]       
        public void method()
        {
            Console.WriteLine("hello world");//this would not be triggled when debug
        }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, January 27, 2021 8:44 AM
  • User387004 posted

    Beautiful! Thanks! that's pretty much what I am looking for. I figured there had to be a simpler way to do it than the older C++ way.

    Cheers!

    Thursday, January 28, 2021 2:38 PM