コード例 #1
0
        private static void RunTest(TestInfo testInfo)
        {
            //Program.Logger.Info($" +++++ Run:  {testInfo.TestMethod}({testInfo.FuncParam ?? ""}), {testInfo.TestRailInfo.CaseID}, {ScreenUrl}");
            Program.Logger.Info($" +++++ Run:  {testInfo.TestMethod}({testInfo.FuncParam ?? ""}), {testInfo.TestRailInfo.CaseID}");

            var classInstance = Activator.CreateInstance(testInfo.TestClass);

            //Program.Logger.Info("--- Test.Setup");
            ExecMethod(classInstance, testInfo.SetupMethod);

            try
            {
                //Program.Logger.Info("--- Test.Run");
                ExecMethod(classInstance, testInfo.TestMethod, testInfo.FuncParam);

                TestRail.SetResult(testInfo.TestRailInfo, ResultStatus.Passed);
                Program.Logger.Info("Success");
            }
            catch (Exception e)
            {
                if (testInfo.TestMethod == null)
                {
                    TestRail.SetResult(testInfo.TestRailInfo, ResultStatus.Failed, e.Message);
                    Program.Logger.Info($"Error: {e.Message}");
                    return;
                }

                var screeenFile = $"{ScreenUrl}/{testInfo.TestMethod.Name}.png";
                var exc         = e.InnerException ?? e;

                var message = exc.Message + Environment.NewLine +
                              screeenFile + Environment.NewLine +
                              exc.StackTrace + Environment.NewLine;

                Program.Logger.Info($"Error: {message}");
                TestRail.SetResult(testInfo.TestRailInfo, ResultStatus.Failed, message);

                WebDriverHelper.CreateScreenshot(testInfo.TestMethod.Name, false);
            }
            finally
            {
                //Program.Logger.Info("--- Test.Teardown");
                ExecMethod(classInstance, testInfo.TeardownMethod);
            }
        }
コード例 #2
0
        private static void RunTest(global::TestRail.Types.Test test)
        {
            MethodInfo method = null;

            var tag          = TestRail.GetTag(test);
            var tagDelimiter = '@';
            var tagVals      = tag.Split(tagDelimiter);

            var funcName  = tag.Contains(tagDelimiter) ? tagVals[0] : tag;
            var funcParam = tag.Contains(tagDelimiter) ? tagVals[1] : null;

            var caseId    = $"C{test.CaseID}";
            var screenUrl = TeamCity.GetScreenUrl();

            Program.Logger.Info($" +++++ Run:  {funcName}-{funcParam??""}, {caseId}, {screenUrl}");

            try
            {
                var type = Asm.GetTypes().First(x => (method = GetMethod(x, funcName, caseId)) != null);

                if (type == null)
                {
                    throw new Exception("[Type] is not defined.");
                }

                if (method == null)
                {
                    throw new Exception("[Method] is not defined.");
                }

                var classInstance = Activator.CreateInstance(type);

                var setupMethod =
                    type.GetMethods()
                    .FirstOrDefault(x => x.GetCustomAttributes().Any(atr => atr.GetType().Name == "SetUpAttribute"));
                var teardownMethod =
                    type.GetMethods()
                    .FirstOrDefault(
                        x => x.GetCustomAttributes().Any(atr => atr.GetType().Name == "TearDownAttribute"));

                if (setupMethod != null)
                {
                    ExecMethod(classInstance, setupMethod);
                }

                ExecMethod(classInstance, method, funcParam);

                //if (teardownMethod != null)
                //    ExecMethod(type, teardownMethod);

                TestRail.SetResult(test, ResultStatus.Passed);
                Program.Logger.Info("Success:");
            }
            catch (Exception e)
            {
                if (method == null)
                {
                    TestRail.SetResult(test, ResultStatus.Failed, e.Message);
                    Program.Logger.Info($"Error: {e.Message}");
                    return;
                }

                var screeenFile = $"{screenUrl}/{method.Name}.png";
                var exc         = e.InnerException ?? e;

                var message = exc.Message + Environment.NewLine +
                              screeenFile + Environment.NewLine +
                              exc.StackTrace + Environment.NewLine;

                Program.Logger.Info($"Error: {message}");
                TestRail.SetResult(test, ResultStatus.Failed, message);

                WebDriverHelper.CreateScreenshot(method.Name, false);
            }
        }