如何从测试项目中引用netcoreapp

时间:2016-12-06 20:07:25

标签: visual-studio-2015 asp.net-core

我需要在我的集成测试项目中启动review the individual explain plans。我想在我的API项目中使用启动类。我想知道是否可以从我的Test项目中添加对我的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%" ]
  }
}

根据Nate的回答编辑。修改了测试项目中的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).

3 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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

来自Test Project的

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;
    }
  }
}

集成测试的一个例子:

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);
    }
  }
}

答案 2 :(得分:0)

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

https://github.com/aspnet/Tooling/issues/538

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

https://www.microsoft.com/net/download/core