Testaviorは、ASP.NETコアの行動テストの開発に役立つ軽量ソリューションです。
動作テストは、機能的なシナリオをカバーするためにさまざまな種類の動作を適用するアプリケーション機能をテストする方法です。
ASP.NETコアアプリケーションの自動テストを作成するためのシンプルで効率的なアプローチを提供します。
ASP.NET Coreを使用した動作テストの詳細については、http://geeklearning.io/a-different-approach-your-asp-net-core-applicationをご覧ください。
Testaviorは2つのライブラリを提供します。
ASP.NETコアプロジェクトについて
> dotnet add package GeekLearning.Testavior.Configuration
.NETコアユニットテストプロジェクトについて
> dotnet add package GeekLearning.Testavior
Testaviorが提供するテスト環境は、生産環境構成をテスト環境構成から分離できるスタートアップ構成サービスに基づいています。この構成サービスは、 3 ConfigureEnvironment - ConfigureServiceのメソッドを定義する契約IStartupConfigurationServiceサービスで表されますConfigure
1-ASP.NETコアプロジェクト:
StartupConfigurationServiceクラス(必要に応じて名前を変更)を追加します。IStartupConfigurationServiceインターフェイスを実装します(オプションで、 DefaultStartupConfigurationServiceから継承して、デフォルトの空の実装を使用します)ConfigureServices :生産環境に固有の構成オプションを実装するConfigure :生産環境に固有のミドルウェア構成を実装するConfigureEnvironment :何よりも前に実行する必要があるものを実装するサンプル:
public class StartupConfigurationService : DefaultStartupConfigurationService
{
public override void ConfigureServices ( IServiceCollection services , IConfigurationRoot configuration )
{
base . ConfigureServices ( services , configuration ) ;
var connection = "CONNECTION_STRING" ;
services . AddDbContext < [ EF_DB_CONTEXT ] > ( options =>
options . UseSqlServer ( connection ) ) ;
}
} 2-プログラムクラス:
WebHostBuilderでConfigureStartupメソッドを呼び出すことにより、 StartupConfigurationServiceを挿入します。
new WebHostBuilder ( )
.. .
. UseStartup < Startup > ( )
. ConfigureStartup < StartupConfigurationService > ( ) 3- Startupクラス:
IStartupConfigurationServiceインターフェイスをStartupクラスに挿入しますStartupコンストラクターの最後にConfigureEnvironmentメソッドを呼び出すStartup.ConfigureServicesメソッドの最後にConfigureServicesメソッドを呼び出すStartup.Configureメソッドの先頭にConfigureメソッドを呼び出すサンプル:
public class Startup
{
private IStartupConfigurationService externalStartupConfiguration ;
public Startup ( IHostingEnvironment env , IStartupConfigurationService externalStartupConfiguration = null )
{
this . externalStartupConfiguration = externalStartupConfiguration ;
this . externalStartupConfiguration . ConfigureEnvironment ( env ) ;
}
public void ConfigureServices ( IServiceCollection services )
{
services . AddMvc ( )
// Pass configuration (IConfigurationRoot) to the configuration service if needed
this . externalStartupConfiguration . ConfigureServices ( services , null ) ;
}
public void Configure ( IApplicationBuilder app , IHostingEnvironment env , ILoggerFactory loggerFactory )
{
this . externalStartupConfiguration . Configure ( app , env , loggerFactory ) ;
app . UseMvc ( ) ;
}
} 4-テストプロジェクトファイル:
Razor Engineは、依存関係ファイル(.deps.json)を使用して、実行時にいくつかの参照を解決します。したがって、アプリケーションのMVC部分をテストするには、これらのファイルをインポートする必要があります。それを行うには、次のセクションを.csprojに追加します。
< Target Name = " CopyDepsFiles " AfterTargets = " Build " Condition = " '$(TargetFramework)'!='' " >
< ItemGroup >
< DepsFilePaths Include = " $([System.IO.Path]::ChangeExtension('%(_ResolvedProjectReferencePaths.FullPath)', '.deps.json')) " />
</ ItemGroup >
< Copy SourceFiles = " %(DepsFilePaths.FullPath) " DestinationFolder = " $(OutputPath) " Condition = " Exists('%(DepsFilePaths.FullPath)') " />
</ Target > 5- Xunitユーザー向け
Xunitを使用する場合は、まず公式の文書に従って、 xunit.runner.jsonファイルをテストプロジェクトに追加します。
{
"shadowCopy" : false
}次のセクションを.csprojに追加します。
< ItemGroup >
< None Include = " xunit.runner.json " CopyToOutputDirectory = " PreserveNewest " />
</ ItemGroup >テスト特定の構成を実装する場合は、テスト環境に特定のIStartupConfigurationServiceサービスが必要です。
Testaviorには、テスト固有のIStartupConfigurationServiceサービスの実装が付属しています。これは、有用な機能でいっぱいのテスト環境を提供するTestStartupConfigurationServiceサービスです(機能セクションを参照)。
もちろん、独自のStartup Configuration Serviceを実装できます(オンボードTestStartupConfigurationServiceを使用するかどうか)。
テスト環境を作成するには、ASP.NETコアアプリケーションStartup 、 IStartupConfigurationServiceサービスの実装、EF Core ObjectContextのタイプ、ASP.NETコアプロジェクトへの相対パス(MVCビューを排除するために必要)を渡すことにより、 TestEnvironmentクラスを導入するだけです。
var testEnvironment = new TestEnvironment < Startup , TestStartupConfigurationService < [ EF_DB_CONTEXT ] > > (
Path . Combine ( System . AppContext . BaseDirectory , @"[PATH_TO_WEB_APP]" ) ) ; テスト環境を使用してWebリクエストを送信するだけでAPIテストを作成します。
[ TestMethod ]
public void ScenarioShouldBeOk ( )
{
var testEnvironment = new TestEnvironment < Startup , TestStartupConfigurationService < [ EF_DB_CONTEXT ] > > (
Path . Combine ( System . AppContext . BaseDirectory , @"[PATH_TO_WEB_APP]" ) ) ;
var response = testEnvironment . Client . GetAsync ( "/api/data" ) . Result ;
response . EnsureSuccessStatusCode ( ) ;
// Test result content
var result = JsonConvert . DeserializeObject < Data [ ] > ( response . Content . ReadAsStringAsync ( ) . Result ) ;
Assert . AreEqual ( "data" , result . Data ) ;
} MVCテストの書き込みは、ビューではなくサーバーによって返されたモデルをテストすることを希望する場合を除き、APIをテストするのとほぼ同じくらい簡単です。
そのために、 Testaviorは、サーバーによって返されるビューのモデルを傍受および保存するViewModelリポジトリを提供します。
ASP.NETコア依存関係注入メカニズムを使用して、このリポジトリにアクセスできます。
[ TestMethod ]
public void ScenarioShouldBeOk ( )
{
var testEnvironment = new TestEnvironment < Startup , TestStartupConfigurationService < [ EF_DB_CONTEXT ] > > (
Path . Combine ( System . AppContext . BaseDirectory , @"[PATH_TO_WEB_APP]" ) ) ;
testEnvironment . Client . GetAsync ( "/" ) . Result . EnsureSuccessStatusCode ( ) ;
var viewModel = testEnvironment
. ServiceProvider
. GetRequiredService < ViewModelRepository > ( )
. Get < [ VIEWMODEL_TYPE ] > ( ) ;
Assert . AreEqual ( "data" , viewModel . Data ) ;
}サンプルセクションを自由に見てください;)
幸せなテスト! :)