public void SqlTestComplete_Events_Raised_When_Test_Complete() { var mockedLoader = Mock<ISqlTestLoader>(); var testFixtures = new List<ISqlTestFixture> { Mock<ISqlTestFixture>(), Mock<ISqlTestFixture>(), Mock<ISqlTestFixture>(), }; var runner = new SqlTestRunner(mockedLoader); var eventCalledCount = 0; // expect the collection to be returned once mockedLoader.Expect(x => x.Fixtures).Return(testFixtures).Repeat.Once(); // expect each fixture to be executed once foreach (var fixture in testFixtures) fixture.Expect(x => x.Execute(null)).IgnoreArguments().Repeat.Once(); Mocks.ReplayAll(); runner.SqlTestComplete += ((sender, e) => { eventCalledCount++; }); using (var con = GetDbConnection()) runner.ExecuteAll(con).ToList(); Assert.That(eventCalledCount, Is.EqualTo(3)); }
private static async Task <TestSummary> RunStoredProcedureTester(StoredProcedureTest storedProcedureTest = null) { if (storedProcedureTest == null) { storedProcedureTest = StoredProcedureTestFactory.DefaultTest(); } SqlTestRunner sqlTestRunner = new SqlTestRunner(new NullLogHelper()); TestSummary testSummary = await sqlTestRunner.Run(storedProcedureTest); return(testSummary); }
public void SqlTestRunComplete_Event_Raised_With_Summary_Results_When_Run_Complete() { var mockedLoader = Mock<ISqlTestLoader>(); var runner = new SqlTestRunner(mockedLoader); var testFixtures = new List<ISqlTestFixture> { Mock<ISqlTestFixture>(), Mock<ISqlTestFixture>(), Mock<ISqlTestFixture>(), }; // expect the collection to be returned once mockedLoader.Expect(x => x.Fixtures).Return(testFixtures).Repeat.Once(); var testRunEventCalledCount = 0; int fixtureExecutedCount = 0; // expect each fixture to be executed once foreach (var fixture in testFixtures) { fixture.Stub(x => x.Execute(null)) .IgnoreArguments() .WhenCalled(x => { // execute with a delay to check we get a TimeTaken result System.Threading.Thread.Sleep(100); // fail one of the tests to check the counts x.ReturnValue = new SqlTestResult(++fixtureExecutedCount % 2 == 1); }); } Mocks.ReplayAll(); SqlTestRunCompleteEventArgs args = null; runner.SqlTestRunComplete += ((sender, e) => { testRunEventCalledCount++; args = e; }); using (var con = GetDbConnection()) runner.ExecuteAll(con).ToList(); Assert.That(testRunEventCalledCount, Is.EqualTo(1)); Assert.That(args, Is.Not.Null); Assert.That(args.TestsTaken, Is.EqualTo(testFixtures.Count)); Assert.That(args.TestsPassed, Is.EqualTo(2)); Assert.That(args.TestsFailed, Is.EqualTo(1)); Assert.That(args.TimeTaken.Ticks, Is.GreaterThan(0)); }
public void Can_Execute_All_Test_Fixtures_From_Runner() { var mockedLoader = Mock<ISqlTestLoader>(); var testFixtures = new List<ISqlTestFixture> { Mock<ISqlTestFixture>(), Mock<ISqlTestFixture>(), Mock<ISqlTestFixture>(), }; // expect the collection to be returned once mockedLoader.Expect(x => x.Fixtures).Return(testFixtures).Repeat.Once(); // expect each fixture to be executed once foreach (var fixture in testFixtures) fixture.Expect(x => x.Execute(null)).IgnoreArguments().Repeat.Once(); Mocks.ReplayAll(); var runner = new SqlTestRunner(mockedLoader); var results = runner.ExecuteAll(null).ToList(); Assert.That(results.Count, Is.EqualTo(testFixtures.Count)); Mocks.VerifyAll(); }
static int Main(string[] args) { var loggerConfiguration = new LoggerConfiguration(); loggerConfiguration.WriteTo.Console(Serilog.Events.LogEventLevel.Debug, theme: AnsiConsoleTheme.Literate, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"); var logger = (ILogger)loggerConfiguration.CreateLogger(); var serializerSettings = new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver(), Formatting = Formatting.Indented }; var app = new CommandLineApplication { Name = "sqltest", Description = "A test runner for sql databases." }; app.HelpOption(HELP_PATTERN); app.Option("-j|--json", "Outputs test results as json", CommandOptionType.NoValue); app.Option("-s|--schema", "Specifies the used schema for the test-procedures", CommandOptionType.SingleValue); app.Option("-u|--user", "Specifies the used user for the test-procedures", CommandOptionType.SingleValue); app.Option("-p|--password", "Specifies the used schema for the test-procedures", CommandOptionType.SingleValue); app.Command("run", runConfig => { runConfig.HelpOption(HELP_PATTERN); var testFileArgument = runConfig.Argument("test file", "One or more files containing test cases", false); var jsonOption = runConfig.Option("-j|--json", "Outputs test results as json", CommandOptionType.NoValue); var schemaOption = runConfig.Option("-s|--schema", "Specifies the used schema for the test-procedures", CommandOptionType.SingleValue); var userOption = runConfig.Option("-u|--user", "Specifies the used user for the test-procedures", CommandOptionType.SingleValue); var passwordOption = runConfig.Option("-p|--password", "Specifies the used schema for the test-procedures", CommandOptionType.SingleValue); runConfig.OnExecute(async() => { if (jsonOption.HasValue()) { logger = Serilog.Core.Logger.None; } var matcher = new Matcher(StringComparison.OrdinalIgnoreCase); matcher.AddInclude(testFileArgument.Value); var files = matcher.GetResultsInFullPath(Environment.CurrentDirectory); try { var testRunner = new SqlTestRunner(logger); var allResults = new List <SqlTestResult>(); foreach (var file in files) { logger.Information("Executing test file {FileName}", file); var testFileContents = File.ReadAllText(file); var testFile = JsonConvert.DeserializeObject <SqlTestFile>(testFileContents, serializerSettings); var schema = ""; if (schemaOption.HasValue()) { schema = schemaOption.Value() + "."; } var user = ""; if (userOption.HasValue()) { user = userOption.Value(); } var password = ""; if (passwordOption.HasValue()) { password = passwordOption.Value(); } var testResults = await testRunner.Run(testFile, schema, user, password); allResults.AddRange(testResults); } if (!jsonOption.HasValue()) { PrintTestResults(allResults, logger); } else { Console.WriteLine(JsonConvert.SerializeObject(allResults, serializerSettings)); } var isSuccess = allResults.TrueForAll(r => r.IsSuccess); return(isSuccess ? 0 : 1); } catch (Exception ex) { logger.Error(ex, "An error occured while executing the test case"); return(1); } }); }); if (args.Length == 0) { app.ShowHelp(); } return(app.Execute(args)); }