diff --git a/UnitTestingDemo.sln b/UnitTestingDemo.sln index 87aa243..ff85a87 100644 --- a/UnitTestingDemo.sln +++ b/UnitTestingDemo.sln @@ -1,23 +1,23 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28307.421 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30011.22 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingDemo", "src\UnitTestingDemo\UnitTestingDemo.csproj", "{55970F10-87CB-4F50-B14B-6AB8258D22DD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestingDemo", "src\UnitTestingDemo\UnitTestingDemo.csproj", "{55970F10-87CB-4F50-B14B-6AB8258D22DD}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingDemo.Data", "src\UnitTestingDemo.Data\UnitTestingDemo.Data.csproj", "{54963324-B96D-4FB5-8B11-E00B9FC9016B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestingDemo.Data", "src\UnitTestingDemo.Data\UnitTestingDemo.Data.csproj", "{54963324-B96D-4FB5-8B11-E00B9FC9016B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingDemo.Services", "src\UnitTestingDemo.Services\UnitTestingDemo.Services.csproj", "{6C2AF4F6-5FDE-4B8D-BD4F-8D0C424E2F1F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestingDemo.Services", "src\UnitTestingDemo.Services\UnitTestingDemo.Services.csproj", "{6C2AF4F6-5FDE-4B8D-BD4F-8D0C424E2F1F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingDemo.Domain", "src\UnitTestingDemo.Domain\UnitTestingDemo.Domain.csproj", "{959885B8-F972-42C6-8E2B-CA910CF643A8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestingDemo.Domain", "src\UnitTestingDemo.Domain\UnitTestingDemo.Domain.csproj", "{959885B8-F972-42C6-8E2B-CA910CF643A8}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{CDB88796-4BCF-4578-89B8-100D596CDBF3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingDemo.Data.Tests", "tests\UnitTestingDemo.Data.Tests\UnitTestingDemo.Data.Tests.csproj", "{EF7E491B-D3AC-4535-BE9D-9F1ECBD20D4F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestingDemo.Data.Tests", "tests\UnitTestingDemo.Data.Tests\UnitTestingDemo.Data.Tests.csproj", "{EF7E491B-D3AC-4535-BE9D-9F1ECBD20D4F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingDemo.Tests", "tests\UnitTestingDemo.Tests\UnitTestingDemo.Tests.csproj", "{511C1EC4-057A-4AF4-A3EF-5563800B3653}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestingDemo.Tests", "tests\UnitTestingDemo.Tests\UnitTestingDemo.Tests.csproj", "{511C1EC4-057A-4AF4-A3EF-5563800B3653}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingDemo.Services.Tests", "tests\UnitTestingDemo.Services.Tests\UnitTestingDemo.Services.Tests.csproj", "{B2998F44-32A3-4987-83F3-307F7A423EF2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestingDemo.Services.Tests", "tests\UnitTestingDemo.Services.Tests\UnitTestingDemo.Services.Tests.csproj", "{B2998F44-32A3-4987-83F3-307F7A423EF2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/azure-pipelines.yml b/azure-pipelines.yml new file mode 100644 index 0000000..cb851df --- /dev/null +++ b/azure-pipelines.yml @@ -0,0 +1,80 @@ +# ASP.NET Core (.NET Framework) +# Build and test ASP.NET Core projects targeting the full .NET Framework. +# Add steps that publish symbols, save build artifacts, and more: +# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core + +trigger: +- master + +pool: + vmImage: 'windows-latest' + +variables: + solution: '**/*.sln' + buildPlatform: 'Any CPU' + buildConfiguration: 'Release' + NUGET_PACKAGES: $(Pipeline.Workspace)/.nuget/packages + +steps: +- task: DotNetCoreCLI@2 + displayName: Restore nuget packages + inputs: + command: restore + projects: '**/*.csproj' + arguments: '--locked-mode' + workingDirectory: $(Build.SourcesDirectory) + +- task: DotNetCoreCLI@2 + displayName: Build + inputs: + command: build + projects: '$(solution)' + arguments: '--configuration $(buildConfiguration)' + +# Run all tests with "/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura" to generate the code coverage file +- task: DotNetCoreCLI@2 + displayName: Test + inputs: + command: test + arguments: '--configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura' + projects: '$(solution)' + nobuild: true + continueOnError: true + +# Generate the report using ReportGenerator (https://github.com/danielpalme/ReportGenerator) +# First install the tool on the machine, then run it +- script: | + dotnet tool install -g dotnet-reportgenerator-globaltool + reportgenerator -reports:$(Build.SourcesDirectory)/tests/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/CodeCoverage -reporttypes:Cobertura + displayName: Create Code coverage report + +# Publish the code coverage result (summary and web site) +# The summary allows to view the coverage percentage in the summary tab +# The web site allows to view which lines are covered directly in Azure Pipeline +- task: PublishCodeCoverageResults@1 + displayName: 'Publish code coverage' + inputs: + codeCoverageTool: Cobertura + summaryFileLocation: '$(Build.SourcesDirectory)/CodeCoverage/Cobertura.xml' + reportDirectory: '$(Build.SourcesDirectory)/CodeCoverage' + pathToSources: '$(Build.SourcesDirectory)' + +- task: PublishTestResults@2 + inputs: + testResultsFormat: 'VSTest' + testResultsFiles: '**/TEST-*.xml' + mergeTestResults: true + failTaskOnFailedTests: true + +- task: DotNetCoreCLI@2 + inputs: + command: publish + publishWebProjects: True + arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)' + zipAfterPublish: True + +- task: PublishBuildArtifacts@1 + inputs: + PathtoPublish: '$(Build.ArtifactStagingDirectory)' + ArtifactName: 'drop' + publishLocation: 'Container' \ No newline at end of file diff --git a/src/UnitTestingDemo.Data/UnitTestingDemo.Data.csproj b/src/UnitTestingDemo.Data/UnitTestingDemo.Data.csproj index b961e9d..ec14690 100644 --- a/src/UnitTestingDemo.Data/UnitTestingDemo.Data.csproj +++ b/src/UnitTestingDemo.Data/UnitTestingDemo.Data.csproj @@ -21,5 +21,4 @@ - diff --git a/src/UnitTestingDemo.Domain/UnitTestingDemo.Domain.csproj b/src/UnitTestingDemo.Domain/UnitTestingDemo.Domain.csproj index d4c395e..19d6c25 100644 --- a/src/UnitTestingDemo.Domain/UnitTestingDemo.Domain.csproj +++ b/src/UnitTestingDemo.Domain/UnitTestingDemo.Domain.csproj @@ -3,5 +3,4 @@ netstandard2.1 - diff --git a/src/UnitTestingDemo.Services/UnitTestingDemo.Services.csproj b/src/UnitTestingDemo.Services/UnitTestingDemo.Services.csproj index ee3d695..c39d1b4 100644 --- a/src/UnitTestingDemo.Services/UnitTestingDemo.Services.csproj +++ b/src/UnitTestingDemo.Services/UnitTestingDemo.Services.csproj @@ -8,5 +8,4 @@ - diff --git a/src/UnitTestingDemo/UnitTestingDemo.csproj b/src/UnitTestingDemo/UnitTestingDemo.csproj index 286d4d5..8ce4919 100644 --- a/src/UnitTestingDemo/UnitTestingDemo.csproj +++ b/src/UnitTestingDemo/UnitTestingDemo.csproj @@ -16,5 +16,4 @@ - diff --git a/tests/UnitTestingDemo.Data.Tests/BookRepositoryTestsWithInMemoryDB.cs b/tests/UnitTestingDemo.Data.Tests/BookRepositoryTestsWithInMemoryDB.cs index bdea486..974c967 100644 --- a/tests/UnitTestingDemo.Data.Tests/BookRepositoryTestsWithInMemoryDB.cs +++ b/tests/UnitTestingDemo.Data.Tests/BookRepositoryTestsWithInMemoryDB.cs @@ -12,28 +12,34 @@ namespace UnitTestingDemo.Data.Tests [TestClass] public class BookRepositoryTestsWithInMemoryDB { - readonly List _books = new List + private readonly AuthorEntity shakespeare = new AuthorEntity { - new BookEntity + Id = 1, + FirstName = "William", + LastName = "Shakespeare", + Books = new List { - Id = 1, - Title = "Hamlet", - Author = new AuthorEntity + new BookEntity { Id = 1, - FirstName = "William", - LastName = "Shakespeare" - } - }, - new BookEntity - { - Id = 2, - Title = "A Midsummer Night's Dream", - Author = new AuthorEntity + Title = "Hamlet", + Author = new AuthorEntity + { + Id = 1, + FirstName = "William", + LastName = "Shakespeare" + } + }, + new BookEntity { - Id = 1, - FirstName = "William", - LastName = "Shakespeare" + Id = 2, + Title = "A Midsummer Night's Dream", + Author = new AuthorEntity + { + Id = 1, + FirstName = "William", + LastName = "Shakespeare" + } } } }; @@ -47,7 +53,7 @@ public void GetAll_WithItems_returnsAll() .Options; using (var context = new BookContext(options)) { - context.Books.AddRange(_books); + context.Authors.Add(shakespeare); context.SaveChanges(); } @@ -60,7 +66,7 @@ public void GetAll_WithItems_returnsAll() } //assert - Assert.AreEqual(_books.Count(), result.Count); + Assert.AreEqual(shakespeare.Books.Count(), result.Count); } } } diff --git a/tests/UnitTestingDemo.Data.Tests/BookRepositoryTestsWithMocks2.cs b/tests/UnitTestingDemo.Data.Tests/BookRepositoryTestsWithMocks2.cs index bb8e03f..f090e49 100644 --- a/tests/UnitTestingDemo.Data.Tests/BookRepositoryTestsWithMocks2.cs +++ b/tests/UnitTestingDemo.Data.Tests/BookRepositoryTestsWithMocks2.cs @@ -1,9 +1,7 @@ using System.Collections.Generic; using System.Linq; -using EntityFrameworkCoreMock; -using Microsoft.EntityFrameworkCore; +using EntityFrameworkCore3Mock; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; using UnitTestingDemo.Data.Contexts; using UnitTestingDemo.Data.EntityModels; @@ -13,7 +11,7 @@ namespace UnitTestingDemo.Data.Tests public class BookRepositoryTestsWithMocks2 { private BookRepository _bookRepository; - private Mock _mockBookContext; + private DbContextMock _mockBookContext; private IQueryable _books; [TestInitialize] @@ -31,7 +29,7 @@ public void TestInitialize() FirstName = "William", LastName = "Shakespeare" } - }, +}, new BookEntity { Id = 2, @@ -45,8 +43,9 @@ public void TestInitialize() } }.AsQueryable(); - var _mockBookContext = new DbContextMock(new DbContextOptionsBuilder().Options); + _mockBookContext = new DbContextMock(); var bookDbSetMock = _mockBookContext.CreateDbSetMock(x => x.Books, _books); + _bookRepository = new BookRepository(_mockBookContext.Object); } diff --git a/tests/UnitTestingDemo.Data.Tests/UnitTestingDemo.Data.Tests.csproj b/tests/UnitTestingDemo.Data.Tests/UnitTestingDemo.Data.Tests.csproj index 13f4184..b0f53c0 100644 --- a/tests/UnitTestingDemo.Data.Tests/UnitTestingDemo.Data.Tests.csproj +++ b/tests/UnitTestingDemo.Data.Tests/UnitTestingDemo.Data.Tests.csproj @@ -1,25 +1,35 @@ + netcoreapp3.1 false - - Library - + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + - diff --git a/tests/UnitTestingDemo.Services.Tests/UnitTestingDemo.Services.Tests.csproj b/tests/UnitTestingDemo.Services.Tests/UnitTestingDemo.Services.Tests.csproj index 761d319..cf87203 100644 --- a/tests/UnitTestingDemo.Services.Tests/UnitTestingDemo.Services.Tests.csproj +++ b/tests/UnitTestingDemo.Services.Tests/UnitTestingDemo.Services.Tests.csproj @@ -1,23 +1,31 @@ + netcoreapp3.1 false - - Library + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + - diff --git a/tests/UnitTestingDemo.Tests/UnitTestingDemo.Tests.csproj b/tests/UnitTestingDemo.Tests/UnitTestingDemo.Tests.csproj index 695ccad..07b44a8 100644 --- a/tests/UnitTestingDemo.Tests/UnitTestingDemo.Tests.csproj +++ b/tests/UnitTestingDemo.Tests/UnitTestingDemo.Tests.csproj @@ -1,19 +1,28 @@  + netcoreapp3.1 false - - Library + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + +