Home > database >  How to start clientapp and run xunit test
How to start clientapp and run xunit test

Time:04-19

I want to make integrated test form my app. I have asp.net core app where I have a angular clientapp. My issue is that the asp.net core project cannot be started when my unit tests are running and I cannot run the unit test when my asp.net core project is runing. How can I in the unit test project start my asp.net core project before the unit tests start. Thank you very much.

Here are the two project erp_colombia.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
    <TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
    <IsPackable>false</IsPackable>
    <SpaRoot>ClientApp\</SpaRoot>
    <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>

    <!-- Set this to true if you enable server-side prerendering -->
    <BuildServerSideRenderer>false</BuildServerSideRenderer>
    <ApplicationIcon />
    <OutputType>WinExe</OutputType>
    <StartupObject />
    <Nullable>annotations</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="EntityFramework" Version="6.4.4" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="3.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.11.1" />
    <PackageReference Include="MySql.Data" Version="8.0.25" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.11.1" />
  </ItemGroup>

  <ItemGroup>
    <!-- Don't publish the SPA source files, but do show them in the project files list -->
    <Content Remove="$(SpaRoot)**" />
    <None Remove="$(SpaRoot)**" />
    <None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
  </ItemGroup>

  <ItemGroup>
    <None Remove="ClientApp\src\app\models\ChatMessage.ts" />
    <None Remove="ClientApp\src\app\models\CommentModel.ts" />
    <None Remove="ClientApp\src\app\models\Component.ts" />
    <None Remove="ClientApp\src\app\models\componentLocation.ts" />
    <None Remove="ClientApp\src\app\models\componentLocationPlainText.ts" />
    <None Remove="ClientApp\src\app\models\Contact.ts" />
    <None Remove="ClientApp\src\app\models\Contract.ts" />
    <None Remove="ClientApp\src\app\models\ContractComponent.ts" />
    <None Remove="ClientApp\src\app\models\ContractMenuEmployee.ts" />
    <None Remove="ClientApp\src\app\models\ContractMenuType.ts" />
    <None Remove="ClientApp\src\app\models\contractOfferModel.ts" />
    <None Remove="ClientApp\src\app\models\contractShipping.ts" />
    <None Remove="ClientApp\src\app\models\country.ts" />
    <None Remove="ClientApp\src\app\models\Employee.ts" />
    <None Remove="ClientApp\src\app\models\EmployeeMenu.ts" />
    <None Remove="ClientApp\src\app\models\Family.ts" />
    <None Remove="ClientApp\src\app\models\generalConfiguration.ts" />
    <None Remove="ClientApp\src\app\models\History.ts" />
    <None Remove="ClientApp\src\app\models\Holiday.ts" />
    <None Remove="ClientApp\src\app\models\inventory.ts" />
    <None Remove="ClientApp\src\app\models\LoginModel.ts" />
    <None Remove="ClientApp\src\app\models\menuType.ts" />
    <None Remove="ClientApp\src\app\models\newsCategory.ts" />
    <None Remove="ClientApp\src\app\models\newsStoryCategory.ts" />
    <None Remove="ClientApp\src\app\models\Quote.ts" />
    <None Remove="ClientApp\src\app\models\review.ts" />
    <None Remove="ClientApp\src\app\models\shipping.ts" />
    <None Remove="ClientApp\src\app\models\step.ts" />
    <None Remove="ClientApp\src\app\models\supplier.ts" />
    <None Remove="ClientApp\src\app\models\type.ts" />
    <None Remove="ClientApp\src\app\models\TypeMenu.ts" />
    <None Remove="Models\login.ts" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="ClientApp\dist\" />
    <Folder Include="ClientApp\src\assets\themes\" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\DAL\DAL.csproj" />
  </ItemGroup>

  <ItemGroup>
    <TypeScriptCompile Include="ClientApp\src\app\models\ChatMessage.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\componentLocation.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\componentLocationPlainText.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\ContractComponent.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\ContractMenuEmployee.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\ContractMenuType.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\contractOfferModel.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\contractShipping.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\country.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\EmployeeMenu.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\Holiday.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\LoginModel.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\menuType.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\Quote.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\step.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\CommentModel.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\Component.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\Contract.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\generalConfiguration.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\Contact.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\Employee.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\Family.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\History.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\inventory.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\newsCategory.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\newsStoryCategory.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\review.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\type.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\shipping.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\supplier.ts" />
    <TypeScriptCompile Include="ClientApp\src\app\models\TypeMenu.ts" />
  </ItemGroup>

  <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="true">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
    <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
  </Target>

  <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod" Condition=" '$(BuildServerSideRenderer)' == 'true' " />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
      <DistFiles Include="$(SpaRoot)node_modules\**" Condition="'$(BuildServerSideRenderer)' == 'true'" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>

</Project>

And here is my test project

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
    <PackageReference Include="Selenium.WebDriver" Version="4.1.0" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="3.0.2">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\DAL\DAL.csproj" />
    <ProjectReference Include="..\erp_colombia\erp_colombia.csproj" />
  </ItemGroup>

</Project>

CodePudding user response:

This is not a scope of unit tests. Unit tests should test only a single method inside your class.

If you want to test functionality of both angular and .net app running at same time, you will need a E2E tests against test environment where your application is deployed, written in one of test frameworks, for example, specflow...

Here are few articles about testing, you could find useful

https://www.geeksforgeeks.org/types-software-testing/

https://www.atlassian.com/continuous-delivery/software-testing/automated-testing

On another topic, you should consider removing angular app from your .net api solution in to it's own project...

CodePudding user response:

One of the possible solution it is just build your asp net web api, run exe file and from your IDE run tests for the target API's URL.

  • Related