美文网首页
.NET DevOps 接入指南 | 创建流水线测试项目

.NET DevOps 接入指南 | 创建流水线测试项目

作者: 圣杰 | 来源:发表于2022-03-20 15:31 被阅读0次
从demos群组,点击新建项目,会有三个选项:创建空白项目、从模板创建或导入项目。这里直接点击创建空白项目: image

这里创建了一个名为AspNetCore.CiCd.Demo的项目,用于后续探索GitLab CI/CD。创建成功后,

紧接着点击+``新建文件,按下图步骤创建.gitignroe文件。

image

创建成功后,点击+新建develop分支,然后点击克隆复制项目地址,将项目克隆到本地,并签出develop分支:

PS C:\Users\shengjie> git clone https://gitlab.shengjie.dev/demos/aspnetcore.cicd.demo.git D:\Gitlab\Demos\AspNetCore.CiCd.Demo
Cloning into 'D:\Gitlab\Demos\AspNetCore.CiCd.Demo'...
PS C:\Users\shengjie> cd D:\Gitlab\Demos\AspNetCore.CiCd.Demo\
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> git flow init #git flow 初始化
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> git flow config # 查看当前git flow 配置
Branch name for production releases: main
Branch name for "next release" development: develop
Feature branch prefix: feature/
Bugfix branch prefix: bugfix/
Release branch prefix: release/
Hotfix branch prefix: hotfix/
Support branch prefix: support/
Version tag prefix
```

接下来通过创建启动一个feature来初始化解决方案,该解决方案包含一个AspNetCore的Web项目和XUnit项目,命令行如下:

```
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> git flow feature start InitialSolution # 启动新Feature
Switched to a new branch 'feature/InitialSolution'
Summary of actions:
- A new branch 'feature/InitialSolution' was created, based on 'develop'
- You are now on branch 'feature/InitialSolution'
Now, start committing on your feature. When done, use:
     git flow feature finish InitialSolution
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> dotnet new sln -n AspNetCore.CiCd.Demo #创建解决方案
The template "Solution File" was created successfully.
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> dotnet new web -n AspNetCore.CiCd.Web #创建Asp.Net Core Web项目
The template "ASP.NET Core Empty" was created successfully.
...
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> dotnet sln add .\AspNetCore.CiCd.Web\ #添加至解决方案
Project `AspNetCore.CiCd.Web\AspNetCore.CiCd.Web.csproj` added to the solution.
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> dotnet new xunit -n AspNetCore.CiCd.Web.Tests #创建XUnit 项目
The template "xUnit Test Project" was created successfully.
...
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> dotnet sln add .\AspNetCore.CiCd.Web.Tests\ #添加至解决方案
Project `AspNetCore.CiCd.Web.Tests\AspNetCore.CiCd.Web.Tests.csproj` added to the solution.    
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> ls
    Directory: D:\Gitlab\Demos\AspNetCore.CiCd.Demo
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        10/16/2021   8:23 PM                AspNetCore.CiCd.Web
d-----        10/16/2021   8:24 PM                AspNetCore.CiCd.Web.Tests
-a----        10/16/2021  11:20 AM           6132 .gitignore
-a----        10/16/2021   8:28 PM           2935 AspNetCore.CiCd.Demo.sln
-a----        10/16/2021  11:04 AM             72 README.md

然后点击AspNetCore.CiCd.Demo.sln在Visual Studio打开解决方案。然后在Web项目中创建一个素数判断静态类:PrimeCheckService类,代码如下:

using System;

namespace AspNetCore.CiCd.Web
{
    public static class PrimeCheckService
    {
        /// <summary>
        /// 素数:大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数。
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static bool IsPrimeNumber(int num)
        {
            throw new NotImplementedException("Not implemented yet!");
        }
    }
}

然后AspNetCore.CiCd.Web.Tests项目添加对AspNetCore.CiCd.Web项目的引用。同时修改UnitTest1.cs,如下所示:

using Xunit;

namespace AspNetCore.CiCd.Web.Tests
{
    public class UnitTest1
    {
        [Fact]
        public void IsPrime_InputIs1_ShouldReturnFalse()
        {
            var isPrime = PrimeCheckService.IsPrimeNumber(1);
            Assert.False(isPrime,"1 不是素数!");
        }
    }
}

由于PrimeCheckService并未给出具体实现,此时若执行单元测试,必定失败。这个问题暂搁置在此,后续在持续构建阶段来修复该问题。完成项目初始化后,关闭feature,并将本地创建项目提交并推送至gitlab,命令如下:

PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> git commit -m 'create aspnetcore cicd demo solution'
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> git flow feature finish
//...
Summary of actions:
- The feature branch 'feature/InitialSolution' was merged into 'develop'
- Feature branch 'feature/InitialSolution' has been locally deleted
- You are now on branch 'develop'
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> git push

推送成功后,打开GitLab中刚创建的项目页面,切换至develop分支,如下图所示。

image

相关文章

网友评论

      本文标题:.NET DevOps 接入指南 | 创建流水线测试项目

      本文链接:https://www.haomeiwen.com/subject/eqtadrtx.html