惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
Jina AI
Jina AI
MyScale Blog
MyScale Blog
N
Netflix TechBlog - Medium
月光博客
月光博客
云风的 BLOG
云风的 BLOG
T
The Blog of Author Tim Ferriss
博客园_首页
GbyAI
GbyAI
The Cloudflare Blog
博客园 - 叶小钗
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
量子位
博客园 - Franky
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
InfoQ
Google DeepMind News
Google DeepMind News
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
J
Java Code Geeks
腾讯CDC
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
S
Schneier on Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
Lohrmann on Cybersecurity
S
Securelist
F
Full Disclosure
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
The GitHub Blog
The GitHub Blog

博客园 - 寻找无名的特质

开源轻量级工作流WorkflowCore介绍 开源的.Net 工作流引擎Elsa初试——创建工作流服务器和图形化工作流配置管理应用 Asp.Net Core Identity 多数据库支持 创建NuGet本地包源 使用Visual Studio 2022开发前端 c# 一些警告的处理方法 Kendo UI Grid 批量编辑使用总结 Kendo UI Grid 使用总结 使用PostMan Canary测试受Identity Server 4保护的Web Api GoJS 使用笔记 Asp.Net Core: Swagger 与 Identity Server 4 VS Code开发TypeScript 使用Xamarin开发移动应用示例——数独游戏(八)使用MVVM实现完成游戏列表页面 使用Xamarin开发移动应用示例——数独游戏(七)添加新游戏 使用Xamarin开发移动应用示例——数独游戏(六)使用数据库 使用Xamarin开发移动应用示例——数独游戏(五)保存游戏进度 使用Xamarin开发移动应用示例——数独游戏(四)产生新游戏算法改进 使用Xamarin开发移动应用示例——数独游戏(三)添加回退和计时功能 使用Xamarin开发移动应用示例——数独游戏(二)创建游戏界面
C# 使用SpecFlow创建BDD测试用例
寻找无名的特质 · 2022-06-16 · via 博客园 - 寻找无名的特质

将自然语言编写的测试用例转换为可执行的测试,可以大大降低需求与开发之间的沟通成本,这是BDD(行为驱动开发)希望达到的效果。SpecFlow是.Net平台的BDD工具,可以帮助我们创建面向BDD的测试用例。

首先,在Visual Studio 2022中安装SpecFlow插件。选择菜单扩展->管理扩展,然后搜索SpecFlow:

点击下载,下载完成后,需要退出Visual Studio 2022,插件会自动安装。

我们编写一个简单的计算BMI(Body Mass Index身体质量指数)的功能作为测试目标,算法很简单,输入是身高和体重,计算公式是体重除以身高的平方。

再次启动Visual Studio,创建一个类库项目,名称为CalBmi,编写代码如下:

namespace CalBmi
{
    public class BmiCalculator
    {
        public Decimal Height { get; set; }

        public Decimal Weight { get; set; }

        public Decimal Bmi()
        {
            throw new NotImplementedException();
        }
    }
}

接下来,在解决方案中添加SpecFlow项目,选择项目类型为SpecFlow:

项目名称为TestBmi,选择xUnit作为Test Framework:

创建完成后,项目的结构是这样的:

然后,添加项目引用,将测试目标项目CalBmi添加到TestBmi的项目引用中:

到这里,准备工作完成,现在可以开始写测试用例了。在TestBmi中有一个示例模板,

代码是这样的:

Feature: Calculator
![Calculator](https://specflow.org/wp-content/uploads/2020/09/calculator.png)
Simple calculator for adding **two** numbers

Link to a feature: [Calculator](TestBmi/Features/Calculator.feature)
***Further read***: **[Learn more about how to generate Living Documentation](https://docs.specflow.org/projects/specflow-livingdoc/en/latest/LivingDocGenerator/Generating-Documentation.html)**

@mytag
Scenario: Add two numbers
	Given the first number is 50
	And the second number is 70
	When the two numbers are added
	Then the result should be 120

这个测试的是两个数字相加,我们照猫画虎,编写我们自己的测试用例,我们添加一个新的SpecFlow feature:

编写代码如下:

Feature: 计算BMI

@mytag
Scenario: 根据身高体重计算BMI
	Given 身高1.75米
	And 体重70.00公斤
	When 计算BMI
	Then 结果应该是22.86

点击右键,选择DefineSteps:

一个新的测试文件被创建了:

代码如下:

using System;
using TechTalk.SpecFlow;

namespace TestBmi.StepDefinitions
{
    [Binding]
    public class 计算BMIStepDefinitions
    {
        [Given(@"身高(.*)米")]
        public void Given身高米(Decimal p0)
        {
            throw new PendingStepException();
        }

        [Given(@"体重(.*)公斤")]
        public void Given体重公斤(Decimal p0)
        {
            throw new PendingStepException();
        }

        [When(@"计算BMI")]
        public void When计算BMI()
        {
            throw new PendingStepException();
        }

        [Then(@"结果应该是(.*)")]
        public void Then结果应该是(Decimal p0)
        {
            throw new PendingStepException();
        }
    }
}

在这个类中,编写测试,首先创建BmiCalculator的实例:

private readonly BmiCalculator _bmical = new BmiCalculator();

然后,改写各个方法:

using CalBmi;
using System;
using TechTalk.SpecFlow;

namespace TestBmi.StepDefinitions
{
    [Binding]
    public class 计算BMIStepDefinitions
    {
        private readonly BmiCalculator _bmical = new BmiCalculator();
        private decimal _result;

        [Given(@"身高(.*)米")]
        public void Given身高米(Decimal p0)
        {
            _bmical.Height = p0;
        }

        [Given(@"体重(.*)公斤")]
        public void Given体重公斤(Decimal p0)
        {
            _bmical.Weight=p0;
        }

        [When(@"计算BMI")]
        public void When计算BMI()
        {
            _result=_bmical.Bmi();
        }

        [Then(@"结果应该是(.*)")]
        public void Then结果应该是(Decimal result)
        {
            _result.Should().Be(result); 
        }
    }
}

在测试管理器中运行这个测试:

与想象的一样,测试没有通过,因为我们没有编写实现代码,现在,修改BmiCalculator ,增加计算方法:

namespace CalBmi
{
    public class BmiCalculator
    {
        public Decimal Height { get; set; }

        public Decimal Weight { get; set; }

        public Decimal Bmi()
        {
            return Weight/Height/Height;
        }
    }
}

再次运行测试:

仍然没有通过,问题是需要保留两位小数,最后一位四舍五入,修改算法:

        public Decimal Bmi()
        {
            return System.Decimal.Round(Weight /Height/Height,2);
        }

再次运行测试,这次通过了:

在测试中给出了测试步骤和每个步骤花费的时间。