Visual studio 2015 如何从测试项目中引用netcoreapp

Visual studio 2015 如何从测试项目中引用netcoreapp,visual-studio-2015,asp.net-core,Visual Studio 2015,Asp.net Core,我需要在我的集成测试项目中启动一个新的测试。我想在我的API项目中使用startup类。我想知道是否可以从我的测试项目中添加对API项目的引用,如果可以,我该如何做。 谢谢 集成测试项目 { "version": "1.0.0-*", "dependencies": { "ProjectManager.Core": "1.0.0-*", "ProjectManager.Domain": "1.0.0-*", "ProjectManager.Gateway": "1

我需要在我的集成测试项目中启动一个新的测试。我想在我的API项目中使用startup类。我想知道是否可以从我的测试项目中添加对API项目的引用,如果可以,我该如何做。 谢谢

集成测试项目

{
  "version": "1.0.0-*",
  "dependencies": {
    "ProjectManager.Core": "1.0.0-*",
    "ProjectManager.Domain": "1.0.0-*",
    "ProjectManager.Gateway": "1.0.0-*",
    "ProjectManager.Model": "1.0.0-*",
    "ProjectManager.Services": "1.0.0-*",
    "ProjectManager.Services.REST": "1.0.0-*",
    "ProjectManager.Services.WCF": "1.0.0-*",
    "NUnit": "3.5.0",
    "dotnet-test-nunit": "3.4.0-beta-3",
    "Moq": "4.6.38-alpha",
    "Autofac": "4.2.1",
    "Microsoft.Extensions.PlatformAbstractions": "1.1.0",
    "NETStandard.Library": "1.6.1",
    "Microsoft.AspNetCore.TestHost": "1.1.0"
  },
  "runtimes": {
    "win10-x64": {}
  },

  "frameworks": {
    "netcoreapp1.1": {
      "imports": [
        "netcoreapp1.1",
        "portable-net45+win8"
      ],
      "dependencies": {
        "Microsoft.NETCore.App": "1.1.0",
        "System.ServiceModel.Duplex": "4.3.0",
        "System.ServiceModel.Http": "4.3.0",
        "System.ServiceModel.NetTcp": "4.3.0",
        "System.ServiceModel.Security": "4.3.0",
        "System.Xml.XmlSerializer": "4.3.0"
      }
    },
    "net462": {
      "frameworkAssemblies": {
        "System.Runtime.Serialization": "4.0.0.0",
        "System.ServiceModel": "4.0.0.0",
        "System.Xml": "4.0.0.0"
      },
      "dependencies": {}
    }
  },
  "testRunner": "nunit"
}
api项目

{
  "dependencies": {
    "ProjectManager.Core": "1.0.0-*",
    "ProjectManager.Domain": "1.0.0-*",
    "ProjectManager.Gateway": "1.0.0-*",
    "ProjectManager.Model": "1.0.0-*",
    "ProjectManager.Services": "1.0.0-*",
    "ProjectManager.Services.REST": "1.0.0-*",
    "Autofac.Extensions.DependencyInjection": "4.0.0",
    "Microsoft.AspNetCore.Mvc": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.AspNetCore.Session": "1.1.0",
    "Microsoft.Extensions.Configuration.CommandLine": "1.1.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
    "Microsoft.Extensions.DependencyInjection": "1.1.0",
    "Microsoft.Extensions.Logging": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.1.0",
    "Microsoft.Extensions.Logging.Debug": "1.1.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
    "Microsoft.NETCore.App": "1.1.0",
    "Autofac": "4.2.1",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
    "Microsoft.Extensions.Configuration.Json": "1.1.0"

  },

  "tools": {
  },
  "runtimes": {
    "win10-x64": {}
  },
  "frameworks": {
    "netcoreapp1.1": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ],
      "dependencies": {}
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "Areas/**/Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}
编辑内特的答案。测试项目中修改的project.json和错误消息:

{
  "version": "1.0.0-*",
  "dependencies": {
    "ProjectManager.Core": "1.0.0-*",
    "ProjectManager.Domain": "1.0.0-*",
    "ProjectManager.Gateway": "1.0.0-*",
    "ProjectManager.Model": "1.0.0-*",
    "ProjectManager.Services": "1.0.0-*",
    "ProjectManager.Services.REST": "1.0.0-*",
    "ProjectManager.Services.WCF": "1.0.0-*",
    "ProjectManager.API": {"target": "project", "version": "1.0.0-*"},
    "NUnit": "3.5.0",
    "dotnet-test-nunit": "3.4.0-beta-3",
    "Moq": "4.6.38-alpha",
    "Autofac": "4.2.1",
    "Microsoft.Extensions.PlatformAbstractions": "1.1.0",
    "NETStandard.Library": "1.6.1",
    "Microsoft.AspNetCore.TestHost": "1.1.0"
  },
  "runtimes": {
    "win10-x64": {}
  },

  "frameworks": {
    "netcoreapp1.1": {
      "imports": [
        "netcoreapp1.1",
        "portable-net45+win8"
      ],
      "dependencies": {
        "Microsoft.NETCore.App": "1.1.0",
        "System.ServiceModel.Duplex": "4.3.0",
        "System.ServiceModel.Http": "4.3.0",
        "System.ServiceModel.NetTcp": "4.3.0",
        "System.ServiceModel.Security": "4.3.0",
        "System.Xml.XmlSerializer": "4.3.0"
      }
    },
    "net462": {
      "frameworkAssemblies": {
        "System.Runtime.Serialization": "4.0.0.0",
        "System.ServiceModel": "4.0.0.0",
        "System.Xml": "4.0.0.0"
      },
      "dependencies": {}
    }
  },
  "testRunner": "nunit"
}
错误消息:

    PATH=C:\Program files\nodejs;.\node_modules\.bin;C:\Program Files (x86)\Microsoft Visual Studio 14.0\Web\External;%PATH%;C:\Program Files (x86)\Microsoft Visual Studio 14.0\Web\External\git
    C:\Program Files\dotnet\dotnet.exe restore "C:\Git\ProjectManager\ProjectManager\.vs\restore.dg"
    log  : Restoring packages for C:\Git\ProjectManager\ProjectManager\src\ProjectManager.Tests\project.json...
    error: Project ProjectManager.API is not compatible with net462 (.NETFramework,Version=v4.6.2). Project ProjectManager.API supports: netcoreapp1.1 (.NETCoreApp,Version=v1.1)
    error: Package Microsoft.NETCore.App 1.1.0 is not compatible with net462 (.NETFramework,Version=v4.6.2). Package Microsoft.NETCore.App 1.1.0 supports: netcoreapp1.0 (.NETCoreApp,Version=v1.0)
    error: One or more projects are incompatible with .NETFramework,Version=v4.6.2.
    error: One or more packages are incompatible with .NETFramework,Version=v4.6.2.
    error: Project ProjectManager.API is not compatible with net462 (.NETFramework,Version=v4.6.2) / win10-x64. Project ProjectManager.API supports: netcoreapp1.1 (.NETCoreApp,Version=v1.1)
    error: Package Microsoft.NETCore.App 1.1.0 is not compatible with net462 (.NETFramework,Version=v4.6.2) / win10-x64. Package Microsoft.NETCore.App 1.1.0 supports: netcoreapp1.0 (.NETCoreApp,Version=v1.0)
    error: One or more projects are incompatible with .NETFramework,Version=v4.6.2 (win10-x64).
    error: One or more packages are incompatible with .NETFramework,Version=v4.6.2 (win10-x64).
    log  : Writing lock file to disk. Path: C:\Git\ProjectManager\ProjectManager\src\ProjectManager.Tests\project.lock.json
    log  : C:\Git\ProjectManager\ProjectManager\src\ProjectManager.Tests\ProjectManager.Tests.xproj
    log  : Restore failed in 5641ms.
    Errors in C:\Git\ProjectManager\ProjectManager\src\ProjectManager.Tests\ProjectManager.Tests.xproj
        Project ProjectManager.API is not compatible with net462 (.NETFramework,Version=v4.6.2). Project ProjectManager.API supports: netcoreapp1.1 (.NETCoreApp,Version=v1.1)
        Package Microsoft.NETCore.App 1.1.0 is not compatible with net462 (.NETFramework,Version=v4.6.2). Package Microsoft.NETCore.App 1.1.0 supports: netcoreapp1.0 (.NETCoreApp,Version=v1.0)
        One or more projects are incompatible with .NETFramework,Version=v4.6.2.
        One or more packages are incompatible with .NETFramework,Version=v4.6.2.
        Project ProjectManager.API is not compatible with net462 (.NETFramework,Version=v4.6.2) / win10-x64. Project ProjectManager.API supports: netcoreapp1.1 (.NETCoreApp,Version=v1.1)
        Package Microsoft.NETCore.App 1.1.0 is not compatible with net462 (.NETFramework,Version=v4.6.2) / win10-x64. Package Microsoft.NETCore.App 1.1.0 supports: netcoreapp1.0 (.NETCoreApp,Version=v1.0)
        One or more projects are incompatible with .NETFramework,Version=v4.6.2 (win10-x64).
        One or more packages are incompatible with .NETFramework,Version=v4.6.2 (win10-x64).
    PATH=C:\Program files\nodejs;.\node_modules\.bin;C:\Program Files (x86)\Microsoft Visual Studio 14.0\Web\External;%PATH%;C:\Program Files (x86)\Microsoft Visual Studio 14.0\Web\External\git
    C:\Program Files\dotnet\dotnet.exe restore "C:\Git\ProjectManager\ProjectManager\.vs\restore.dg"
    log  : Restoring packages for C:\Git\ProjectManager\ProjectManager\src\ProjectManager.Tests\project.json...
    error: Project ProjectManager.API is not compatible with net462 (.NETFramework,Version=v4.6.2). Project ProjectManager.API supports: netcoreapp1.1 (.NETCoreApp,Version=v1.1)
    error: Package Microsoft.NETCore.App 1.1.0 is not compatible with net462 (.NETFramework,Version=v4.6.2). Package Microsoft.NETCore.App 1.1.0 supports: netcoreapp1.0 (.NETCoreApp,Version=v1.0)
    error: One or more projects are incompatible with .NETFramework,Version=v4.6.2.
    error: One or more packages are incompatible with .NETFramework,Version=v4.6.2.
    error: Project ProjectManager.API is not compatible with net462 (.NETFramework,Version=v4.6.2) / win10-x64. Project ProjectManager.API supports: netcoreapp1.1 (.NETCoreApp,Version=v1.1)
    error: Package Microsoft.NETCore.App 1.1.0 is not compatible with net462 (.NETFramework,Version=v4.6.2) / win10-x64. Package Microsoft.NETCore.App 1.1.0 supports: netcoreapp1.0 (.NETCoreApp,Version=v1.0)
    error: One or more projects are incompatible with .NETFramework,Version=v4.6.2 (win10-x64).
    error: One or more packages are incompatible with .NETFramework,Version=v4.6.2 (win10-x64).
    log  : Lock file has not changed. Skipping lock file write. Path: C:\Git\ProjectManager\ProjectManager\src\ProjectManager.Tests\project.lock.json
    log  : C:\Git\ProjectManager\ProjectManager\src\ProjectManager.Tests\ProjectManager.Tests.xproj
    log  : Restore failed in 5266ms.
    Errors in C:\Git\ProjectManager\ProjectManager\src\ProjectManager.Tests\ProjectManager.Tests.xproj
        Project ProjectManager.API is not compatible with net462 (.NETFramework,Version=v4.6.2). Project ProjectManager.API supports: netcoreapp1.1 (.NETCoreApp,Version=v1.1)
        Package Microsoft.NETCore.App 1.1.0 is not compatible with net462 (.NETFramework,Version=v4.6.2). Package Microsoft.NETCore.App 1.1.0 supports: netcoreapp1.0 (.NETCoreApp,Version=v1.0)
        One or more projects are incompatible with .NETFramework,Version=v4.6.2.
        One or more packages are incompatible with .NETFramework,Version=v4.6.2.
        Project ProjectManager.API is not compatible with net462 (.NETFramework,Version=v4.6.2) / win10-x64. Project ProjectManager.API supports: netcoreapp1.1 (.NETCoreApp,Version=v1.1)
        Package Microsoft.NETCore.App 1.1.0 is not compatible with net462 (.NETFramework,Version=v4.6.2) / win10-x64. Package Microsoft.NETCore.App 1.1.0 supports: netcoreapp1.0 (.NETCoreApp,Version=v1.0)
        One or more projects are incompatible with .NETFramework,Version=v4.6.2 (win10-x64).
        One or more packages are incompatible with .NETFramework,Version=v4.6.2 (win10-x64).

假设您的项目位于同一目录树中(例如,在根项目目录下的
src/
test/
文件夹中),将其添加到集成测试项目的
依赖项
部分:

"MyApiProject": {
  "target": "project"
}

target:project
标志告诉NuGet跳过查找包并引用本地项目。

假设您的项目位于同一目录树中(例如,在根项目目录下的
src/
test/
文件夹中),将其添加到集成测试项目的
依赖项
部分:

"MyApiProject": {
  "target": "project"
}

target:project
标志告诉NuGet跳过查找包并引用本地项目。

我在测试中使用
xUnit
,但我也在测试项目中添加了对API的引用。 我要发布我的项目配置。看看它是否对你有帮助。 如果您想查看完整的代码,请选中此项

来自测试项目的project.json:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "WebApp": "1.0.0-*",
    "xunit": "2.2.0-beta2-build3300",
    "dotnet-test-xunit": "2.2.0-preview2-build1029",
    "Microsoft.AspNetCore.TestHost": "1.0.0"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      }
    }
  }
}
请注意,
WebApp
是我的API

将服务器注入测试用例的TestServerFixture类:

public class TestServerFixture : IDisposable {
  private TestServer testServer;
  protected TestServer TestServer {
    get {
      if (testServer == null) {
        testServer = new TestServer(new WebHostBuilder().UseStartup<Startup>());
      }
      return testServer;
    }
  }

  public HttpClient Client {
    get {
      return TestServer.CreateClient();
    }
  }
  public void Dispose() {
    if (testServer != null) {
      testServer.Dispose();
      testServer = null;
    }
  }
}
公共类TestServerFixture:IDisposable{
专用测试服务器;
受保护的TestServer TestServer{
得到{
if(testServer==null){
testServer=newTestServer(newWebHostBuilder().UseStartup());
}
返回testServer;
}
}
公共HttpClient客户端{
得到{
返回TestServer.CreateClient();
}
}
公共空间处置(){
if(testServer!=null){
Dispose();
testServer=null;
}
}
}
集成测试的一个示例:

public class PersonControllerTest : IClassFixture<TestServerFixture> {
  public PersonControllerTest(TestServerFixture fixture) {
    Fixture = fixture;
  }

  protected TestServerFixture Fixture { get; private set; }

  [Fact]
  public async void ItShouldReturnSomething() {
    using (var client = Fixture.Client) {
      var response = await client.GetAsync("/api/person");
      string json = response.Content.ReadAsStringAsync().Result;
      IList<PersonModel> list = JsonConvert.DeserializeObject<IList<PersonModel>>(json);
      Assert.True(list.Count == 3);
    }
  }
}
公共类PersonControllerTest:IClassFixture{
公共人员控制器测试(TestServerFixture fixture){
夹具=夹具;
}
受保护的TestServerFixture Fixture{get;private set;}
[事实]
公共异步void ItShouldReturnSomething(){
使用(var client=Fixture.client){
var response=await client.GetAsync(“/api/person”);
字符串json=response.Content.ReadAsStringAsync().Result;
IList list=JsonConvert.DeserializeObject(json);
Assert.True(list.Count==3);
}
}
}

我在测试中使用了
xUnit
,但我也在测试项目中添加了对API的引用。 我要发布我的项目配置。看看它是否对你有帮助。 如果您想查看完整的代码,请选中此项

来自测试项目的project.json:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "WebApp": "1.0.0-*",
    "xunit": "2.2.0-beta2-build3300",
    "dotnet-test-xunit": "2.2.0-preview2-build1029",
    "Microsoft.AspNetCore.TestHost": "1.0.0"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      }
    }
  }
}
请注意,
WebApp
是我的API

将服务器注入测试用例的TestServerFixture类:

public class TestServerFixture : IDisposable {
  private TestServer testServer;
  protected TestServer TestServer {
    get {
      if (testServer == null) {
        testServer = new TestServer(new WebHostBuilder().UseStartup<Startup>());
      }
      return testServer;
    }
  }

  public HttpClient Client {
    get {
      return TestServer.CreateClient();
    }
  }
  public void Dispose() {
    if (testServer != null) {
      testServer.Dispose();
      testServer = null;
    }
  }
}
公共类TestServerFixture:IDisposable{
专用测试服务器;
受保护的TestServer TestServer{
得到{
if(testServer==null){
testServer=newTestServer(newWebHostBuilder().UseStartup());
}
返回testServer;
}
}
公共HttpClient客户端{
得到{
返回TestServer.CreateClient();
}
}
公共空间处置(){
if(testServer!=null){
Dispose();
testServer=null;
}
}
}
集成测试的一个示例:

public class PersonControllerTest : IClassFixture<TestServerFixture> {
  public PersonControllerTest(TestServerFixture fixture) {
    Fixture = fixture;
  }

  protected TestServerFixture Fixture { get; private set; }

  [Fact]
  public async void ItShouldReturnSomething() {
    using (var client = Fixture.Client) {
      var response = await client.GetAsync("/api/person");
      string json = response.Content.ReadAsStringAsync().Result;
      IList<PersonModel> list = JsonConvert.DeserializeObject<IList<PersonModel>>(json);
      Assert.True(list.Count == 3);
    }
  }
}
公共类PersonControllerTest:IClassFixture{
公共人员控制器测试(TestServerFixture fixture){
夹具=夹具;
}
受保护的TestServerFixture Fixture{get;private set;}
[事实]
公共异步void ItShouldReturnSomething(){
使用(var client=Fixture.client){
var response=await client.GetAsync(“/api/person”);
字符串json=response.Content.ReadAsStringAsync().Result;
IList list=JsonConvert.DeserializeObject(json);
Assert.True(list.Count==3);
}
}
}

我无法实现Nate提供的答案的原因是我遇到了这里描述的问题:

答案是重新安装此处找到的sdk:


我无法实现Nate提供的答案的原因是我遇到了这里描述的问题:

答案是重新安装此处找到的sdk:


添加到依赖项是否需要指定运行时:win10-x64?这通常是不必要的。另外,当我删除运行时并在Microsoft.NETCore.App IIS中使用type:platform失败时,为什么集成测试项目的目标是
net462
以及
netcoreapp1.1
。另外,我有一个WPF项目,它依赖于与API项目相同的模型和服务。顺便说一句,我愿意尝试任何东西。我已经做了几天了。修复A中断B等。添加到依赖项是否需要指定运行时:win10-x64?这通常是不必要的。另外,当我删除运行时并在Microsoft.NETCore.App IIS中使用type:platform失败时,为什么集成测试项目的目标是
net462
以及
netcoreapp1.1
。另外,我有一个WPF项目,它依赖于与API项目相同的模型和服务。顺便说一句,我愿意尝试任何东西。我已经做了几天了。修好A、B等等。谢谢你,内特。我实现了你的答案。我的问题中显示了修改后的project.json和error msg。另外,项目也在同一目录树中。谢谢你。我实现了你的答案。我的问题中显示了修改后的project.json和error msg。此外,项目位于同一目录树中。感谢您的回答,但不幸的是,它没有工作。很好的教程,顺便说一句。添加参考不起作用?你和xUnit试过了吗?谢谢你的回答,但不幸的是,它没有起作用。不错的教程