当前,我在Visual Studio中创建了一个单元测试项目,以使用C#,Selenium Web Driver和SpecFlow自动化测试。
用BDD编写.feature文件并生成步骤定义后,运行所有测试时,它们在测试管理器中无法识别,并且在IntelliSense中出现以下错误。
[Failure] Could not find file 'C:\Users\peter01\source\repos\AutomatedTests\ExampleProject\UnitTest1.cs'.
我的项目已经配置好输出类型“类库”,并且在创建项目时删除了UnitTest1.cs文件。
我需要测试项目才能识别并运行我的Login.feature文件中的所有测试
我的项目只有一个功能文件“ Login.feature”
Feature: Login
Scenario: Login Success
Given i access website
When do login with user
| user | password|
| user123 | pass123 |
Then show message
和LoginStepDefinition.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using TechTalk.SpecFlow;
namespace ExampleNamespaceXXXX
{
[Binding]
public class LoginSteps
{
[Given(@"i access website")]
public void Example1()
{
example line code
}
[When(@"do login with user")]
public void Example2(Table table)
{
try
{
example line code
}
catch (NoSuchElementException)
{
example line code
}
}
[Then(@"show message")]
public void Example3()
{
example line code
}
}
}
[如果您想让VS检测所有测试,则需要使用测试适配器,
如果要使用MStest运行器,则将MSTest.TestAdapter和MSTest.TestFramework作为依赖项添加到您的项目中。
如果要SpecRunner,则将SpecRun.Runner包添加到packages.configSpecFlow项目的文件。您可能需要重新启动VisualStudio查看您的测试。
有关更多参考,您可以通过官方链接:https://specflow.org/documentation/Troubleshooting-Visual-Studio-Integration/
创建类型为MSTest的新项目。 VS to create new project
输入项目名称和位置以保存您的测试项目。
- 这次,您将能够看到项目中的所有基本依赖关系。 default dependencies
- 现在只需通过Nuget包管理器将这两个依赖项添加到您的项目范围中,以使测试运行程序检测到并运行您所有的功能文件。 SpecFlow和SpecFlow.Tools.MsBuild.Generation
现在所有基本配置已完成,您需要构建项目以运行测试项目。