示例#1
0
        /// <summary>
        /// Logs the 'Before Action' info (The method being called and it's properties/parameters).
        /// </summary>
        /// <param name="properties">(Optional) A collection of properties to log ("Method Name" and "Stack Caller" are automatically generated so do not include).</param>
        public static void BeforeAction(OrderedDictionary properties = null)
        {
            // Get the padding (if any) to prepend to the log line
            LogPadding logPadding = new LogPadding(new StackTrace().GetFrame(2).GetMethod().ReflectedType);
            // Get the method name (of the method that called this one) from the stack
            string methodName = new StackTrace().GetFrame(1).GetMethod().ReflectedType + "." + new StackTrace().GetFrame(1).GetMethod().Name + "()";
            // Get that method's caller from the stack
            string stackCaller = new StackTrace().GetFrame(2).GetMethod().ReflectedType + "." + new StackTrace().GetFrame(2).GetMethod().Name + "()";

            // Write the method name
            Log.WriteLine(logPadding.Padding + methodName);
            if (properties != null)
            {
                // Parse the Dictionary (for properties of the method being logged)
                foreach (DictionaryEntry entry in properties)
                {
                    // Write the property to the log
                    Log.WriteLine(logPadding.InfoPadding + "[INFO] " + entry.Key + " : " + entry.Value);
                }
            }
            // Write the stack caller to the log
            Log.WriteLine(logPadding.InfoPadding + "[STACK] Caller: " + stackCaller);
        }
示例#2
0
        /// <summary>
        /// Verifies that two objects are not equal.
        /// </summary>
        /// <param name="expected">The objects that is expected.</param>
        /// <param name="actual">The objects value to compare.</param>
        /// <param name="message">The message to display in case of failure.</param>
        public static void AreNotEqual(object expected, object actual, string message = "")
        {
            // Figure out the padding (if any) to prepend to the log line
            LogPadding logPadding = new LogPadding(new StackTrace().GetFrame(1).GetMethod().ReflectedType);
            // Logging - Before action
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(logPadding.Padding + "Assert.AreNotEqual(expected, actual, message?)");
            sb.AppendLine(logPadding.InfoPadding + "[PARAM] Expected: " + expected);
            sb.AppendLine(logPadding.InfoPadding + "[PARAM] Actual: " + actual);
            sb.AppendLine(logPadding.InfoPadding + "[STACK] Caller: " + new StackTrace().GetFrame(1).GetMethod().ReflectedType + "." + new StackTrace().GetFrame(1).GetMethod().Name + "()");
            Log.Write(sb.ToString());
            // Perform the action
            try
            {
                // Verify that two objects are not equal
                NUnit.Framework.Assert.AreNotEqual(expected, actual);
                // Logging - After action success
                Log.Success(logPadding.Padding);
            }
            catch (Exception e)
            {
                // Logging - After action exception
                if (message != "")
                {
                    sb.AppendLine(logPadding.InfoPadding + "[INFO] Message: " + message);
                }
                sb = Log.Exception(sb, e);
                // Fail current Test
                Fail(sb.ToString());
            }
            finally
            {
                // Logging - After action
                Log.Finally(logPadding.Padding);
            }
        }
示例#3
0
        /// <summary>
        /// A convenience method for logging a "[RESULT] Failure: {message}".
        /// </summary>
        /// <param name="message">(Optional) The message to display in the log's Result line.</param>
        /// <param name="exception"></param>
        public static void Failure(string message = "", Exception exception = null)
        {
            // Get the padding (if any)
            LogPadding logPadding = new LogPadding(new StackTrace().GetFrame(2).GetMethod().ReflectedType);

            // Write the line to the log
            if (message != "")
            {
                WriteLine(logPadding.Padding + "[RESULT] Failure: " + message);
            }
            else if (exception != null)
            {
                WriteLine(logPadding.Padding + "[RESULT] Failure: " + exception.Message);
            }
            else
            {
                WriteLine(logPadding.Padding + "[RESULT] Failure!");
            }
            // If there is no padding, then write an end line to sepearte the actions in the log
            if (logPadding.Padding == "")
            {
                Log.WriteLine();
            }
        }
示例#4
0
        /// <summary>
        /// Suspends the current thread for the specified number of milliseconds.
        /// </summary>
        /// <param name="milliseconds">The number of milliseconds to sleep.</param>
        /// <param name="reason">The reason for the wait.</param>
        public static void Milliseconds(int milliseconds, string reason = "")
        {
            // Figure out the padding (if any) to prepend to the log line
            LogPadding logPadding = new LogPadding(new StackTrace().GetFrame(1).GetMethod().ReflectedType);
            // Logging - Before action
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(logPadding.Padding + "Sleep.Milliseconds(milliseconds, reason?)");
            sb.AppendLine(logPadding.InfoPadding + "[PARAM] Milliseconds: " + milliseconds);
            if (reason != "")
            {
                sb.AppendLine(logPadding.InfoPadding + "[PARAM] Reason: " + reason);
            }
            sb.AppendLine(logPadding.InfoPadding + "[STACK] Caller: " + new StackTrace().GetFrame(1).GetMethod().ReflectedType + "." + new StackTrace().GetFrame(1).GetMethod().Name + "()");
            Log.Write(sb.ToString());
            // Perform the action
            try
            {
                // Suspend the current thread for the specified number of milliseconds
                System.Threading.Thread.Sleep(milliseconds);
                // Logging - After action success
                Log.Success(logPadding.Padding);
            }
            catch (Exception e)
            {
                // Logging - After action exception
                sb = Log.Exception(sb, e);
                // Fail current Test
                Assert.Fail(sb.ToString());
            }
            finally
            {
                // Logging - After action
                Log.Finally(logPadding.Padding);
            }
        }