.net 核心单元测试未 运行 正确
.netcore unit testing not running properly
我已经为一个简单的 c#/dotnet-core 控制台应用程序创建了一些单元测试。
我已经在测试项目上创建了参考,但由于某种原因 "dotnet test" cmd 仍然报告以下错误:
UnitTest1.cs(12,25): error CS0103: The name 'LinkChecker' does not
exist in the current context
[/Users/med.bensalem/Desktop/dotnet-projects/checklinksTests/checklinksTests.csproj]
UnitTest1.cs(19,25): error CS0103: The name 'LinkChecker' does not
exist in the current context
[/Users/med.bensalem/Desktop/dotnet-projects/checklinksTests/checklinksTests.csproj]
项目结构:
.
├── checklinksTests
│ ├── UnitTest1.cs
│ ├── checklinksTests.csproj
└── checklinksconsole
└── code
├── LinkChecker.cs
├── Program.cs
└── checklinksconsole.csproj
请在下面找到我项目源代码的代码:
主应用程序 - checklinksconsole:
checklinksconsole.csproj - 文件:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="HtmlAgilityPack.NetCore" Version="1.5.0.1" />
</ItemGroup>
</Project>
Program.cs - 文件:
using System;
using System.Linq;
using System.Net.Http;
namespace checklinksconsole
{
class Program
{
static void Main(string[] args)
{
var site = "https://www.google.com";
var client = new HttpClient();
var body = client.GetStringAsync(site);
Console.WriteLine(body.Result);
Console.WriteLine();
Console.WriteLine("Links:");
var links = LinkChecker.GetLinks(body.Result);
links.ToList().ForEach(Console.WriteLine);
}
}
}
LinkChecker.cs - 文件:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using HtmlAgilityPack;
namespace checklinksconsole
{
public class LinkChecker
{
public static IEnumerable<string> GetLinks(string page)
{
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(page);
var links = htmlDocument.DocumentNode.SelectNodes("//a[@href]")
.Select(n => n.GetAttributeValue("href", string.Empty))
.Where(l => !String.IsNullOrEmpty(l))
.Where(l => l.StartsWith("http"));
return links;
}
}
}
单元测试 - checklinksTests:
checklinksTests.csproj - 文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\checklinksconsole\code\checklinksconsole.csproj" />
</ItemGroup>
</Project>
UnitTest1.cs - 文件:
using System;
using Xunit;
namespace checklinksTests
{
public class UnitTest1
{
[Fact]
public void WithoutHTTPAtTheStartOfTheLink_NoLinks()
{
var links = LinkChecker.GetLinks("<a href=\"google.com\" />");
Assert.Equal(links.Count(),0);
}
[Fact]
public void WithHTTPAtTheStartOfTheLink_LinkParses()
{
var links = LinkChecker.GetLinks("<a href=\"http://google.com\" />");
Assert.Equal(links.Count(),1);
Assert.Equal(links.First(), "google.com");
}
}
}
知道我的代码有什么问题。
谢谢
您在测试 class 的顶部缺少 using checklinksconsole;
指令以将 LinkChecker
class 纳入范围。
我已经为一个简单的 c#/dotnet-core 控制台应用程序创建了一些单元测试。 我已经在测试项目上创建了参考,但由于某种原因 "dotnet test" cmd 仍然报告以下错误:
UnitTest1.cs(12,25): error CS0103: The name 'LinkChecker' does not exist in the current context [/Users/med.bensalem/Desktop/dotnet-projects/checklinksTests/checklinksTests.csproj] UnitTest1.cs(19,25): error CS0103: The name 'LinkChecker' does not exist in the current context [/Users/med.bensalem/Desktop/dotnet-projects/checklinksTests/checklinksTests.csproj]
项目结构:
.
├── checklinksTests
│ ├── UnitTest1.cs
│ ├── checklinksTests.csproj
└── checklinksconsole
└── code
├── LinkChecker.cs
├── Program.cs
└── checklinksconsole.csproj
请在下面找到我项目源代码的代码:
主应用程序 - checklinksconsole:
checklinksconsole.csproj - 文件:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="HtmlAgilityPack.NetCore" Version="1.5.0.1" />
</ItemGroup>
</Project>
Program.cs - 文件:
using System;
using System.Linq;
using System.Net.Http;
namespace checklinksconsole
{
class Program
{
static void Main(string[] args)
{
var site = "https://www.google.com";
var client = new HttpClient();
var body = client.GetStringAsync(site);
Console.WriteLine(body.Result);
Console.WriteLine();
Console.WriteLine("Links:");
var links = LinkChecker.GetLinks(body.Result);
links.ToList().ForEach(Console.WriteLine);
}
}
}
LinkChecker.cs - 文件:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using HtmlAgilityPack;
namespace checklinksconsole
{
public class LinkChecker
{
public static IEnumerable<string> GetLinks(string page)
{
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(page);
var links = htmlDocument.DocumentNode.SelectNodes("//a[@href]")
.Select(n => n.GetAttributeValue("href", string.Empty))
.Where(l => !String.IsNullOrEmpty(l))
.Where(l => l.StartsWith("http"));
return links;
}
}
}
单元测试 - checklinksTests:
checklinksTests.csproj - 文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\checklinksconsole\code\checklinksconsole.csproj" />
</ItemGroup>
</Project>
UnitTest1.cs - 文件:
using System;
using Xunit;
namespace checklinksTests
{
public class UnitTest1
{
[Fact]
public void WithoutHTTPAtTheStartOfTheLink_NoLinks()
{
var links = LinkChecker.GetLinks("<a href=\"google.com\" />");
Assert.Equal(links.Count(),0);
}
[Fact]
public void WithHTTPAtTheStartOfTheLink_LinkParses()
{
var links = LinkChecker.GetLinks("<a href=\"http://google.com\" />");
Assert.Equal(links.Count(),1);
Assert.Equal(links.First(), "google.com");
}
}
}
知道我的代码有什么问题。
谢谢
您在测试 class 的顶部缺少 using checklinksconsole;
指令以将 LinkChecker
class 纳入范围。