Exemple #1
0
        static int Main(string[] args)
        {
            bool showHelp = false;

            Console.CancelKeyPress += new ConsoleCancelEventHandler(CancelKeyPressHandler);
            log4net.GlobalContext.Properties["LogLocation"] = AppDomain.CurrentDomain.BaseDirectory + "\\logs";
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));

            OptionSet options = new OptionSet()
                                .Add("v=|TcUnitVerifierPath=", "Path to TcUnit-Verifier TwinCAT solution", v => tcUnitVerifierPath = v)
                                .Add("?|h|help", h => showHelp = h != null);

            try
            {
                options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `TcUnit-Verifier --help' for more information.");
                return(Constants.RETURN_ERROR);
            }

            /* Make sure the user has supplied the path for the Visual Studio solution file.
             * Also verify that this file exists.
             */
            if (showHelp || tcUnitVerifierPath == null)
            {
                DisplayHelp(options);
                return(Constants.RETURN_ERROR);
            }
            if (!File.Exists(tcUnitVerifierPath))
            {
                log.Error("TcUnit-verifier solution " + tcUnitVerifierPath + " does not exist.");
                return(Constants.RETURN_ERROR);
            }

            MessageFilter.Register();

            log.Info("Starting TcUnit-Verifier...");
            try
            {
                vsInstance = new VisualStudioInstance(@tcUnitVerifierPath);
                vsInstance.Load();
            }
            catch
            {
                log.Error("Solution load failed");  // Detailed error messages output by vsInstance.Load()
                CleanUp();
                return(Constants.RETURN_ERROR);
            }

            if (vsInstance.GetVisualStudioVersion() == null)
            {
                log.Error("Did not find Visual studio version in Visual studio solution file.");
                CleanUp();
                return(Constants.RETURN_ERROR);
            }

            log.Info("Cleaning and building TcUnit-Verifier_TwinCAT solution...");
            AutomationInterface automationInterface = new AutomationInterface(vsInstance);

            automationInterface.ITcSysManager.SetTargetNetId("127.0.0.1.1.1");
            ITcSmTreeItem plcProject = automationInterface.PlcTreeItem.Child[1];
            ITcPlcProject iecProject = (ITcPlcProject)plcProject;

            log.Info("Generating TcUnit-Verifier_TwinCAT boot project...");
            iecProject.GenerateBootProject(true);
            iecProject.BootProjectAutostart = true;

            log.Info("Activating TcUnit-Verifier_TwinCAT configuration...");
            automationInterface.ITcSysManager.ActivateConfiguration();

            log.Info("Restarting TwinCAT...");
            automationInterface.ITcSysManager.StartRestartTwinCAT();

            // Wait until tests have been running and are finished
            bool testsFinishedRunningFirstLineFound = false;
            bool amountOfTestSuitesLineFound        = false;
            bool amountOfTestsLineFound             = false;
            bool amountOfSuccesfulTestsLineFound    = false;
            bool amountOfFailedTestsLineFound       = false;
            bool testsFinishedRunningLastLineFound  = false;
            int  numberOfFailedTests = 0;

            log.Info("Waiting for TcUnit-Verifier_TwinCAT to finish running tests...");

            ErrorList errorList = new ErrorList();

            while (true)
            {
                Thread.Sleep(1000);

                ErrorItems errorItems = vsInstance.GetErrorItems();
                log.Info("... got " + errorItems.Count + " report lines so far.");

                var newErrors = errorList.AddNew(errorItems);

                foreach (var error in newErrors.Where(e => e.ErrorLevel == vsBuildErrorLevel.vsBuildErrorLevelHigh))
                {
                    if (error.Description.Contains("| ==========TESTS FINISHED RUNNING=========="))
                    {
                        testsFinishedRunningFirstLineFound = true;
                    }
                    if (error.Description.Contains("| TEST SUITES:"))
                    {
                        amountOfTestSuitesLineFound = true;
                    }
                    if (error.Description.Contains("| TESTS:"))
                    {
                        amountOfTestsLineFound = true;
                    }
                    if (error.Description.Contains("| SUCCESSFUL TESTS:"))
                    {
                        amountOfSuccesfulTestsLineFound = true;
                    }
                    if (error.Description.Contains("| FAILED TESTS:"))
                    {
                        amountOfFailedTestsLineFound = true;
                        // Grab the number of failed tests so we can validate it during the assertion phase
                        numberOfFailedTests = Int32.Parse(error.Description.Split().Last());
                    }
                    if (error.Description.Contains("| ======================================"))
                    {
                        testsFinishedRunningLastLineFound = true;
                    }
                }

                if (testsFinishedRunningFirstLineFound && amountOfTestSuitesLineFound && amountOfTestsLineFound && amountOfSuccesfulTestsLineFound &&
                    amountOfFailedTestsLineFound && testsFinishedRunningLastLineFound)
                {
                    break;
                }
            }

            log.Info("Asserting results...");

            if (numberOfFailedTests != expectedNumberOfFailedTests)
            {
                log.Error("The number of tests that failed (" + numberOfFailedTests + ") does not match expectations (" + expectedNumberOfFailedTests + ")");
            }

            List <ErrorList.Error> errors = new List <ErrorList.Error>(errorList.Where(e => e.ErrorLevel == vsBuildErrorLevel.vsBuildErrorLevelHigh));

            /* Insert the test classes here */
            new FB_PrimitiveTypes(errors);
            new FB_AssertTrueFalse(errors);
            new FB_AssertEveryFailedTestTwice(errors);
            new FB_CreateFourTestsWithSameName(errors);
            new FB_ArrayPrimitiveTypes(errors);
            new FB_CreateDisabledTest(errors);
            new FB_AnyPrimitiveTypes(errors);
            new FB_AssertEveryFailedTestTwiceArrayVersion(errors);
            new FB_AnyToUnionValue(errors);
            new FB_MultipleAssertWithSameParametersInSameCycleWithSameTest(errors);
            new FB_MultipleAssertWithSameParametersInDifferentCyclesButWithDifferentTests(errors);
            new FB_MultipleAssertWithSameParametersInDifferentCyclesAndInSameTest(errors);
            new FB_SkipAssertionsWhenFinished(errors);
            new FB_AdjustAssertFailureMessageToMax252CharLengthTest(errors);
            new FB_CheckIfSpecificTestIsFinished(errors);
            new FB_TestFinishedNamed(errors);

            log.Info("Done.");

            CleanUp();

            return(Constants.RETURN_SUCCESSFULL);
        }
        static int Main(string[] args)
        {
            bool showHelp = false;

            Console.CancelKeyPress += new ConsoleCancelEventHandler(CancelKeyPressHandler);
            log4net.GlobalContext.Properties["LogLocation"] = AppDomain.CurrentDomain.BaseDirectory + "\\logs";
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));

            OptionSet options = new OptionSet()
                                .Add("v=|TcUnitVerifierPath=", "Path to TcUnit-Verifier TwinCAT solution", v => tcUnitVerifierPath = v)
                                .Add("?|h|help", h => showHelp = h != null);

            try
            {
                options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `TcUnit-Verifier --help' for more information.");
                return(Constants.RETURN_ERROR);
            }

            /* Make sure the user has supplied the path for the Visual Studio solution file.
             * Also verify that this file exists.
             */
            if (showHelp || tcUnitVerifierPath == null)
            {
                DisplayHelp(options);
                return(Constants.RETURN_ERROR);
            }
            if (!File.Exists(tcUnitVerifierPath))
            {
                log.Error("TcUnit-verifier solution " + tcUnitVerifierPath + " does not exist.");
                return(Constants.RETURN_ERROR);
            }

            MessageFilter.Register();

            log.Info("Starting TcUnit-Verifier...");
            try
            {
                vsInstance = new VisualStudioInstance(@tcUnitVerifierPath);
                vsInstance.Load();
            }
            catch
            {
                log.Error("Error loading VS DTE. Is the correct version of Visual Studio installed?");
                CleanUp();
                return(Constants.RETURN_ERROR);
            }

            if (vsInstance.GetVisualStudioVersion() == null)
            {
                log.Error("Did not find Visual studio version in Visual studio solution file.");
                CleanUp();
                return(Constants.RETURN_ERROR);
            }

            log.Info("Cleaning and building TcUnit-Verifier_TwinCAT solution...");
            AutomationInterface automationInterface = new AutomationInterface(vsInstance);

            automationInterface.ITcSysManager.SetTargetNetId("127.0.0.1.1.1");
            ITcSmTreeItem plcProject = automationInterface.PlcTreeItem.Child[1];
            ITcPlcProject iecProject = (ITcPlcProject)plcProject;

            log.Info("Generating TcUnit-Verifier_TwinCAT boot project...");
            iecProject.GenerateBootProject(true);
            iecProject.BootProjectAutostart = true;

            log.Info("Activating TcUnit-Verifier_TwinCAT configuration...");
            automationInterface.ITcSysManager.ActivateConfiguration();

            log.Info("Restaring TwinCAT...");
            automationInterface.ITcSysManager.StartRestartTwinCAT();

            // Wait until tests have been running and are finished
            bool testsFinishedRunningFirstLineFound = false;
            bool amountOfTestSuitesLineFound        = false;
            bool amountOfTestsLineFound             = false;
            bool amountOfSuccesfulTestsLineFound    = false;
            bool amountOfFailedTestsLineFound       = false;
            bool testsFinishedRunningLastLineFound  = false;

            log.Info("Waiting for TcUnit-Verifier_TwinCAT to finish running tests...");

            ErrorItems errorItems;

            while (true)
            {
                Thread.Sleep(1000);

                errorItems = vsInstance.GetErrorItems();

                for (int i = 1; i <= errorItems.Count; i++)
                {
                    ErrorItem item = errorItems.Item(i);
                    if (item.ErrorLevel == vsBuildErrorLevel.vsBuildErrorLevelHigh)
                    {
                        if (item.Description.ToUpper().Contains("| ==========TESTS FINISHED RUNNING=========="))
                        {
                            testsFinishedRunningFirstLineFound = true;
                        }
                        if (item.Description.ToUpper().Contains("| TEST SUITES:"))
                        {
                            amountOfTestSuitesLineFound = true;
                        }
                        if (item.Description.ToUpper().Contains("| TESTS:"))
                        {
                            amountOfTestsLineFound = true;
                        }
                        if (item.Description.ToUpper().Contains("| SUCCESSFUL TESTS:"))
                        {
                            amountOfSuccesfulTestsLineFound = true;
                        }
                        if (item.Description.ToUpper().Contains("| FAILED TESTS:"))
                        {
                            amountOfFailedTestsLineFound = true;
                        }
                        if (item.Description.ToUpper().Contains("| ======================================"))
                        {
                            testsFinishedRunningLastLineFound = true;
                        }
                    }
                }

                if (testsFinishedRunningFirstLineFound && amountOfTestSuitesLineFound && amountOfTestsLineFound && amountOfSuccesfulTestsLineFound &&
                    amountOfFailedTestsLineFound && testsFinishedRunningLastLineFound)
                {
                    break;
                }
            }

            log.Info("Asserting results...");

            /* Insert the test classes here */
            FB_PrimitiveTypes                         primitiveTypes                         = new FB_PrimitiveTypes(errorItems, "PrimitiveTypes");
            FB_AssertTrueFalse                        assertTrueFalse                        = new FB_AssertTrueFalse(errorItems, "AssertTrueFalse");
            FB_AssertEveryFailedTestTwice             assertEveryFailedTestTwice             = new FB_AssertEveryFailedTestTwice(errorItems, "AssertEveryFailedTestTwice");
            FB_CreateFourTestsWithSameName            createFourTestsWithSameName            = new FB_CreateFourTestsWithSameName(errorItems, "CreateFourTestsWithSameName");
            FB_ArrayPrimitiveTypes                    arrayPrimitiveTypes                    = new FB_ArrayPrimitiveTypes(errorItems, "ArrayPrimitiveTypes");
            FB_CreateDisabledTest                     createDisabledTest                     = new FB_CreateDisabledTest(errorItems, "CreateDisabledTest");
            FB_AnyPrimitiveTypes                      anyPrimitiveTypes                      = new FB_AnyPrimitiveTypes(errorItems, "AnyPrimitiveTypes");
            FB_AssertEveryFailedTestTwiceArrayVersion assertEveryFailedTestTwiceArrayVersion = new FB_AssertEveryFailedTestTwiceArrayVersion(errorItems, "AssertEveryFailedTestTwiceArrayVersion");
            FB_AnyToUnionValue                        anyToUnionValue                        = new FB_AnyToUnionValue(errorItems, "AnyToUnionValue");
            FB_MultipleAssertWithSameParametersInSameCycleWithSameTest multipleAssertWithSameParametersInSameCycleWithSameTest = new FB_MultipleAssertWithSameParametersInSameCycleWithSameTest(errorItems, "MultipleAssertWithSameParametersInSameCycleWithSameTest");
            FB_MultipleAssertWithSameParametersInDifferentCyclesButWithDifferentTests multipleAssertWithSameParametersInDifferentCyclesButWithDifferentTests = new FB_MultipleAssertWithSameParametersInDifferentCyclesButWithDifferentTests(errorItems, "MultipleAssertWithSameParametersInDifferentCyclesButWithDifferentTests");
            FB_MultipleAssertWithSameParametersInDifferentCyclesAndInSameTest         multipleAssertWithSameParametersInDifferentCyclesAndInSameTest         = new FB_MultipleAssertWithSameParametersInDifferentCyclesAndInSameTest(errorItems, "MultipleAssertWithSameParametersInDifferentCyclesAndInSameTest");
            FB_AdjustAssertFailureMessageToMax252CharLengthTest adjustAssertFailureMessageToMax252CharLengthTest = new FB_AdjustAssertFailureMessageToMax252CharLengthTest(errorItems, "AdjustAssertFailureMessageToMax252CharLengthTest");
            FB_CheckIfSpecificTestIsFinished checkIfSpecificTestIsFinished = new FB_CheckIfSpecificTestIsFinished(errorItems, "CheckIfSpecificTestIsFinished");

            log.Info("Done.");

            CleanUp();

            return(Constants.RETURN_SUCCESSFULL);
        }