Skip to content

Commit 8582c52

Browse files
authored
Set up CI with Azure Pipelines (#4)
* Set up CI with Azure Pipelines * added coverlet for test coverage reporting * updated references * fix unit tests * updated packages
1 parent cfdc130 commit 8582c52

File tree

11 files changed

+143
-59
lines changed

11 files changed

+143
-59
lines changed

UnitTestingDemo.sln

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.28307.421
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30011.22
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingDemo", "src\UnitTestingDemo\UnitTestingDemo.csproj", "{55970F10-87CB-4F50-B14B-6AB8258D22DD}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestingDemo", "src\UnitTestingDemo\UnitTestingDemo.csproj", "{55970F10-87CB-4F50-B14B-6AB8258D22DD}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingDemo.Data", "src\UnitTestingDemo.Data\UnitTestingDemo.Data.csproj", "{54963324-B96D-4FB5-8B11-E00B9FC9016B}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestingDemo.Data", "src\UnitTestingDemo.Data\UnitTestingDemo.Data.csproj", "{54963324-B96D-4FB5-8B11-E00B9FC9016B}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingDemo.Services", "src\UnitTestingDemo.Services\UnitTestingDemo.Services.csproj", "{6C2AF4F6-5FDE-4B8D-BD4F-8D0C424E2F1F}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestingDemo.Services", "src\UnitTestingDemo.Services\UnitTestingDemo.Services.csproj", "{6C2AF4F6-5FDE-4B8D-BD4F-8D0C424E2F1F}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingDemo.Domain", "src\UnitTestingDemo.Domain\UnitTestingDemo.Domain.csproj", "{959885B8-F972-42C6-8E2B-CA910CF643A8}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestingDemo.Domain", "src\UnitTestingDemo.Domain\UnitTestingDemo.Domain.csproj", "{959885B8-F972-42C6-8E2B-CA910CF643A8}"
1313
EndProject
1414
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{CDB88796-4BCF-4578-89B8-100D596CDBF3}"
1515
EndProject
16-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingDemo.Data.Tests", "tests\UnitTestingDemo.Data.Tests\UnitTestingDemo.Data.Tests.csproj", "{EF7E491B-D3AC-4535-BE9D-9F1ECBD20D4F}"
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestingDemo.Data.Tests", "tests\UnitTestingDemo.Data.Tests\UnitTestingDemo.Data.Tests.csproj", "{EF7E491B-D3AC-4535-BE9D-9F1ECBD20D4F}"
1717
EndProject
18-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingDemo.Tests", "tests\UnitTestingDemo.Tests\UnitTestingDemo.Tests.csproj", "{511C1EC4-057A-4AF4-A3EF-5563800B3653}"
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestingDemo.Tests", "tests\UnitTestingDemo.Tests\UnitTestingDemo.Tests.csproj", "{511C1EC4-057A-4AF4-A3EF-5563800B3653}"
1919
EndProject
20-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingDemo.Services.Tests", "tests\UnitTestingDemo.Services.Tests\UnitTestingDemo.Services.Tests.csproj", "{B2998F44-32A3-4987-83F3-307F7A423EF2}"
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestingDemo.Services.Tests", "tests\UnitTestingDemo.Services.Tests\UnitTestingDemo.Services.Tests.csproj", "{B2998F44-32A3-4987-83F3-307F7A423EF2}"
2121
EndProject
2222
Global
2323
GlobalSection(SolutionConfigurationPlatforms) = preSolution

azure-pipelines.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# ASP.NET Core (.NET Framework)
2+
# Build and test ASP.NET Core projects targeting the full .NET Framework.
3+
# Add steps that publish symbols, save build artifacts, and more:
4+
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core
5+
6+
trigger:
7+
- master
8+
9+
pool:
10+
vmImage: 'windows-latest'
11+
12+
variables:
13+
solution: '**/*.sln'
14+
buildPlatform: 'Any CPU'
15+
buildConfiguration: 'Release'
16+
NUGET_PACKAGES: $(Pipeline.Workspace)/.nuget/packages
17+
18+
steps:
19+
- task: DotNetCoreCLI@2
20+
displayName: Restore nuget packages
21+
inputs:
22+
command: restore
23+
projects: '**/*.csproj'
24+
arguments: '--locked-mode'
25+
workingDirectory: $(Build.SourcesDirectory)
26+
27+
- task: DotNetCoreCLI@2
28+
displayName: Build
29+
inputs:
30+
command: build
31+
projects: '$(solution)'
32+
arguments: '--configuration $(buildConfiguration)'
33+
34+
# Run all tests with "/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura" to generate the code coverage file
35+
- task: DotNetCoreCLI@2
36+
displayName: Test
37+
inputs:
38+
command: test
39+
arguments: '--configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura'
40+
projects: '$(solution)'
41+
nobuild: true
42+
continueOnError: true
43+
44+
# Generate the report using ReportGenerator (https://github.com/danielpalme/ReportGenerator)
45+
# First install the tool on the machine, then run it
46+
- script: |
47+
dotnet tool install -g dotnet-reportgenerator-globaltool
48+
reportgenerator -reports:$(Build.SourcesDirectory)/tests/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/CodeCoverage -reporttypes:Cobertura
49+
displayName: Create Code coverage report
50+
51+
# Publish the code coverage result (summary and web site)
52+
# The summary allows to view the coverage percentage in the summary tab
53+
# The web site allows to view which lines are covered directly in Azure Pipeline
54+
- task: PublishCodeCoverageResults@1
55+
displayName: 'Publish code coverage'
56+
inputs:
57+
codeCoverageTool: Cobertura
58+
summaryFileLocation: '$(Build.SourcesDirectory)/CodeCoverage/Cobertura.xml'
59+
reportDirectory: '$(Build.SourcesDirectory)/CodeCoverage'
60+
pathToSources: '$(Build.SourcesDirectory)'
61+
62+
- task: PublishTestResults@2
63+
inputs:
64+
testResultsFormat: 'VSTest'
65+
testResultsFiles: '**/TEST-*.xml'
66+
mergeTestResults: true
67+
failTaskOnFailedTests: true
68+
69+
- task: DotNetCoreCLI@2
70+
inputs:
71+
command: publish
72+
publishWebProjects: True
73+
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
74+
zipAfterPublish: True
75+
76+
- task: PublishBuildArtifacts@1
77+
inputs:
78+
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
79+
ArtifactName: 'drop'
80+
publishLocation: 'Container'

src/UnitTestingDemo.Data/UnitTestingDemo.Data.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@
2121
<ItemGroup>
2222
<ProjectReference Include="..\UnitTestingDemo.Domain\UnitTestingDemo.Domain.csproj" />
2323
</ItemGroup>
24-
2524
</Project>

src/UnitTestingDemo.Domain/UnitTestingDemo.Domain.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
55
</PropertyGroup>
6-
76
</Project>

src/UnitTestingDemo.Services/UnitTestingDemo.Services.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@
88
<ProjectReference Include="..\UnitTestingDemo.Domain\UnitTestingDemo.Domain.csproj" />
99
<ProjectReference Include="..\UnitTestingDemo.Data\UnitTestingDemo.Data.csproj" />
1010
</ItemGroup>
11-
1211
</Project>

src/UnitTestingDemo/UnitTestingDemo.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@
1616
<ProjectReference Include="..\UnitTestingDemo.Services\UnitTestingDemo.Services.csproj" />
1717
<ProjectReference Include="..\UnitTestingDemo.Domain\UnitTestingDemo.Domain.csproj" />
1818
</ItemGroup>
19-
2019
</Project>

tests/UnitTestingDemo.Data.Tests/BookRepositoryTestsWithInMemoryDB.cs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,34 @@ namespace UnitTestingDemo.Data.Tests
1212
[TestClass]
1313
public class BookRepositoryTestsWithInMemoryDB
1414
{
15-
readonly List<BookEntity> _books = new List<BookEntity>
15+
private readonly AuthorEntity shakespeare = new AuthorEntity
1616
{
17-
new BookEntity
17+
Id = 1,
18+
FirstName = "William",
19+
LastName = "Shakespeare",
20+
Books = new List<BookEntity>
1821
{
19-
Id = 1,
20-
Title = "Hamlet",
21-
Author = new AuthorEntity
22+
new BookEntity
2223
{
2324
Id = 1,
24-
FirstName = "William",
25-
LastName = "Shakespeare"
26-
}
27-
},
28-
new BookEntity
29-
{
30-
Id = 2,
31-
Title = "A Midsummer Night's Dream",
32-
Author = new AuthorEntity
25+
Title = "Hamlet",
26+
Author = new AuthorEntity
27+
{
28+
Id = 1,
29+
FirstName = "William",
30+
LastName = "Shakespeare"
31+
}
32+
},
33+
new BookEntity
3334
{
34-
Id = 1,
35-
FirstName = "William",
36-
LastName = "Shakespeare"
35+
Id = 2,
36+
Title = "A Midsummer Night's Dream",
37+
Author = new AuthorEntity
38+
{
39+
Id = 1,
40+
FirstName = "William",
41+
LastName = "Shakespeare"
42+
}
3743
}
3844
}
3945
};
@@ -47,7 +53,7 @@ public void GetAll_WithItems_returnsAll()
4753
.Options;
4854
using (var context = new BookContext(options))
4955
{
50-
context.Books.AddRange(_books);
56+
context.Authors.Add(shakespeare);
5157
context.SaveChanges();
5258
}
5359

@@ -60,7 +66,7 @@ public void GetAll_WithItems_returnsAll()
6066
}
6167

6268
//assert
63-
Assert.AreEqual(_books.Count(), result.Count);
69+
Assert.AreEqual(shakespeare.Books.Count(), result.Count);
6470
}
6571
}
6672
}

tests/UnitTestingDemo.Data.Tests/BookRepositoryTestsWithMocks2.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using EntityFrameworkCoreMock;
4-
using Microsoft.EntityFrameworkCore;
3+
using EntityFrameworkCore3Mock;
54
using Microsoft.VisualStudio.TestTools.UnitTesting;
6-
using Moq;
75
using UnitTestingDemo.Data.Contexts;
86
using UnitTestingDemo.Data.EntityModels;
97

@@ -13,7 +11,7 @@ namespace UnitTestingDemo.Data.Tests
1311
public class BookRepositoryTestsWithMocks2
1412
{
1513
private BookRepository _bookRepository;
16-
private Mock<BookContext> _mockBookContext;
14+
private DbContextMock<BookContext> _mockBookContext;
1715
private IQueryable<BookEntity> _books;
1816

1917
[TestInitialize]
@@ -31,7 +29,7 @@ public void TestInitialize()
3129
FirstName = "William",
3230
LastName = "Shakespeare"
3331
}
34-
},
32+
},
3533
new BookEntity
3634
{
3735
Id = 2,
@@ -45,8 +43,9 @@ public void TestInitialize()
4543
}
4644
}.AsQueryable();
4745

48-
var _mockBookContext = new DbContextMock<BookContext>(new DbContextOptionsBuilder<BookContext>().Options);
46+
_mockBookContext = new DbContextMock<BookContext>();
4947
var bookDbSetMock = _mockBookContext.CreateDbSetMock(x => x.Books, _books);
48+
5049
_bookRepository = new BookRepository(_mockBookContext.Object);
5150
}
5251

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3+
34
<PropertyGroup>
45
<TargetFramework>netcoreapp3.1</TargetFramework>
56

67
<IsPackable>false</IsPackable>
7-
8-
<OutputType>Library</OutputType>
98
</PropertyGroup>
109

1110
<ItemGroup>
12-
<PackageReference Include="coverlet.collector" Version="1.2.1">
13-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14-
<PrivateAssets>all</PrivateAssets>
15-
</PackageReference>
1611
<PackageReference Include="coverlet.msbuild" Version="2.8.1">
1712
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1813
<PrivateAssets>all</PrivateAssets>
1914
</PackageReference>
20-
<PackageReference Include="EntityFrameworkCoreMock.Moq" Version="1.0.0.30" />
15+
<PackageReference Include="EntityFrameworkCore3Mock.Moq" Version="1.0.0.3" />
16+
<PackageReference Include="EntityFrameworkCore3Mock.Shared" Version="1.0.0.3" />
17+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
2118
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.3" />
2219
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.3" />
2320
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
2421
<PackageReference Include="Moq" Version="4.13.1" />
22+
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
2523
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
24+
<PackageReference Include="coverlet.collector" Version="1.2.1">
25+
<PrivateAssets>all</PrivateAssets>
26+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
27+
</PackageReference>
2628
</ItemGroup>
2729

30+
2831
<ItemGroup>
2932
<ProjectReference Include="..\..\src\UnitTestingDemo.Data\UnitTestingDemo.Data.csproj" />
3033
<ProjectReference Include="..\..\src\UnitTestingDemo.Domain\UnitTestingDemo.Domain.csproj" />
3134
</ItemGroup>
32-
3335
</Project>
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3+
34
<PropertyGroup>
45
<TargetFramework>netcoreapp3.1</TargetFramework>
56

67
<IsPackable>false</IsPackable>
7-
8-
<OutputType>Library</OutputType>
98
</PropertyGroup>
109

1110
<ItemGroup>
12-
<PackageReference Include="coverlet.collector" Version="1.2.1">
13-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14-
<PrivateAssets>all</PrivateAssets>
15-
</PackageReference>
1611
<PackageReference Include="coverlet.msbuild" Version="2.8.1">
1712
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1813
<PrivateAssets>all</PrivateAssets>
1914
</PackageReference>
2015
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
2116
<PackageReference Include="Moq" Version="4.13.1" />
17+
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
2218
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
19+
<PackageReference Include="coverlet.collector" Version="1.2.1">
20+
<PrivateAssets>all</PrivateAssets>
21+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
22+
</PackageReference>
2323
</ItemGroup>
2424

25+
2526
<ItemGroup>
2627
<ProjectReference Include="..\..\src\UnitTestingDemo.Domain\UnitTestingDemo.Domain.csproj" />
2728
<ProjectReference Include="..\..\src\UnitTestingDemo.Data\UnitTestingDemo.Data.csproj" />
2829
<ProjectReference Include="..\..\src\UnitTestingDemo.Services\UnitTestingDemo.Services.csproj" />
2930
</ItemGroup>
30-
3131
</Project>

0 commit comments

Comments
 (0)