Ejemplo n.º 1
0
        public void AddOrUpdateVariables_should_update_variable_when_already_set()
        {
            // Arrange
            var sessionVariables = new SessionVariables();

            // Act
            sessionVariables.AddOrUpdateVariables(new Dictionary<string, string>()
            {
                {"nano", "leaf"},
                {"light", "bulb"},
            });
            sessionVariables.AddOrUpdateVariables(new Dictionary<string, string>()
            {
                {"nano", "leaf2"},
                {"light", "bulb2"}
            });

            // Assert
            Assert.That(sessionVariables.GetVariableValue("nano"), Is.EqualTo("leaf2"));
            Assert.That(sessionVariables.GetVariableValue("light"), Is.EqualTo("bulb2"));
        }
Ejemplo n.º 2
0
        internal TestCaseResult RunCase(Case testCase, SessionVariables variables, VerificationsMatcher verificationMatcher)
        {
            var testResult = new TestCaseResult();
            testResult.TestCase = testCase;

            try
            {
                string resolvedUrl = variables.ReplacePlainTextVariablesIn(testCase.Url);
                testResult.ActualUrl = resolvedUrl;

                HttpResponse response = _httpClient.ExecuteRequest(testCase.Method, resolvedUrl, testCase.PostType, testCase.PostBody, testCase.Headers);
                testResult.ResponseTime = response.ResponseTime;
                testResult.HttpResponse = response;

                if (response.StatusCode == testCase.VerifyResponseCode)
                {
                    testResult.ResponseCodeSuccess = true;
                    string content = response.ToString();

                    // Put the parseresponse regex values in the current variable set
                    Dictionary<string, string> parsedVariables = ParseResponseMatcher.MatchParseResponses(testCase.ParseResponses, content);
                    variables.AddOrUpdateVariables(parsedVariables);

                    // Verify positives
                    testResult.VerifyPositiveResults = verificationMatcher.MatchPositive(testCase.VerifyPositives, content);

                    // Verify Negatives
                    testResult.VerifyNegativeResults = verificationMatcher.MatchNegative(testCase.VerifyNegatives, content);
                }
                else
                {
                    testResult.ResponseCodeSuccess = false;
                }

                if (testResult.Success == false)
                {
                    testResult.Message = testCase.ErrorMessage;
                }

                if (ShouldLogRequest(testResult, testCase))
                {
                    _httpClient.LogLastRequest();
                }

                if (ShouldLogResponse(testResult, testCase))
                {
                    _httpClient.LogLastResponse();
                }

                _resultWriter.Write(testResult);

                if (testCase.Sleep > 0)
                    Thread.Sleep(testCase.Sleep * 1000);
            }
            catch (Exception ex)
            {
                testResult.ResponseCodeSuccess = false;
                testResult.ExceptionMessage = ex.Message;
            }

            return testResult;
        }
Ejemplo n.º 3
0
        public TestCaseSession Run(CaseCollection testCollection)
        {
            _isStopPending = false;
            _currentResults = new List<TestCaseResult>();

            var session = new TestCaseSession();
            session.StartTime = DateTime.UtcNow;

            // Add all config variables and ones in this <testcase>
            var variables = new SessionVariables();
            variables.AddGlobalVariables(_config);
            variables.AddOrUpdateVariables(testCollection.Variables);

            var verificationsMatcher = new VerificationsMatcher(variables);

            // Ensure we loop atleast once:
            int repeatTotal = (testCollection.Repeat > 0) ? testCollection.Repeat : 1;
            List<Case> testCases = testCollection.TestCases.ToList();

            TimeSpan minResponseTime = TimeSpan.MaxValue;
            TimeSpan maxResponseTime = TimeSpan.MinValue;
            int totalCasesRun = 0;

            CasesRun = 0;
            TotalCases = testCases.Count;
            RepeatCount = 0;

            for (int i = 0; i < repeatTotal; i++)
            {
                foreach (Case testCase in testCases)
                {
                    if (_isStopPending)
                        break;

                    try
                    {
                        TestCaseResult result = RunCase(testCase, variables, verificationsMatcher);
                        session.TestCaseResults.Add(result);
                        _currentResults.Add(result);

                        if (result.ResponseTime < minResponseTime)
                            minResponseTime = result.ResponseTime;

                        if (result.ResponseTime > maxResponseTime)
                            maxResponseTime = result.ResponseTime;
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, "An exception occurred running case {0}", testCase.Id);
                    }
                    finally
                    {
                        totalCasesRun++;

                        CasesRun++;
                        RepeatCount = i;
                    }
                }

                if (_isStopPending)
                    break;
            }

            session.EndTime = DateTime.UtcNow;
            session.TotalRunTime = session.EndTime - session.StartTime;
            session.TotalCasesRun = totalCasesRun;
            session.MinResponseTime = minResponseTime;
            session.MaxResponseTime = maxResponseTime;

            return session;
        }