xunitPlus 1.2.4

dotnet add package xunitPlus --version 1.2.4
NuGet\Install-Package xunitPlus -Version 1.2.4
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="xunitPlus" Version="1.2.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add xunitPlus --version 1.2.4
#r "nuget: xunitPlus, 1.2.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install xunitPlus as a Cake Addin
#addin nuget:?package=xunitPlus&version=1.2.4

// Install xunitPlus as a Cake Tool
#tool nuget:?package=xunitPlus&version=1.2.4

Inkslab

GitHub language codeSize

xunit+”是什么?

xunit+ 是基于“xunit”再结合“inkslab”对构造函数参数进行自动注入的单元测试包。

如何安装?

First, install NuGet. Then, install xunit+ from the package manager console:

PM> Install-Package xunitPlus

如何使用?

  • 通用模式:

    • 单元测试类的构造函数正常注入即可。
  • 自定义模式:

    • 自定义启动类。
    ///<summary>
    /// 启动类(可选)。
    ///</summary>
    public class Startup {
    
      ///<summary>
      /// 创建宿主构建器(可选)。
      ///</summary>
      public IHostBuilder CreateHostBuilder() => Host.CreateDefaultBuilder();
    
      ///<summary>
      /// 配置宿主构建器(可选)。
      ///</summary>
      public void ConfigureHost(IHostBuilder builder){
          //TODO: 配置构建器。
      }
    
      ///<summary>
      /// 配置依赖注入的服务及生命周期(可选)。
      ///</summary>
      public void ConfigureServices(IServiceCollection services){
          //TODO: 配置依赖注入。
      }
    
      ///<summary>
      /// 构建宿主(可选)。
      ///</summary>
      public IHost BuildHost(IHostBuilder builder) => builder.Build();
    
      ///<summary>
      /// 配置服务(可选)。
      ///</summary>
      public void Configure(/* 参数将被自动依赖注入。 */){
          //TODO: 配置服务。
      }
    }
    
    1. 启动类可以支持唯一的构造函数参数类型为 Type 表示当前运行的单元测试类。
    2. 启动类的所有方法均为可选,返回值类型为 void 的不可更改,否则方法返回值相同或返回值必须是指定接口的实现类。
    3. 方法均是可选方法。
    4. 不区分是否是静态方法。
    5. 不区分是否是静态类。
    • 启动类查找规则:权重从上到下依次 递减
      • 指定启动类。

        ///<summary>
        /// 指定类。
        ///</summary>
        public class SpecifyStartup { 
        
            ///<summary>
            /// 配置依赖注入的服务及生命周期(可选)。
            ///</summary>
            public void ConfigureServices(IServiceCollection services){
                // 注入需要的参数。
                services.AddTransient<ITest, Test>();
            }
        }
        
        ///<summary>
        /// 指定类测试。
        ///</summary>
        [Startup(typeof(SpecifyStartup))]
        public class SpecifyStartupTests(ITest test) {
        
            ///<summary>
            /// 测试。
            ///</summary>
            [Fact]
            public void Test() {
                Assert.True(test is Test);
            }
        }
        

        在单元测试类上通过 StartupAttribute 标记。

      • 单元测试类的内嵌 Startup 类。

        ///<summary>
        /// 内嵌类测试。
        ///</summary>
        public class NestedStartupTests(ITest test) {
        
            ///<summary>
            /// 测试。
            ///</summary>
            [Fact]
            public void Test() {
                Assert.True(test is Test);
            }
        
            ///<summary>
            /// 内嵌类。
            ///</summary>
            public class Startup { 
        
                ///<summary>
                /// 配置依赖注入的服务及生命周期(可选)。
                ///</summary>
                public void ConfigureServices(IServiceCollection services){
                    // 注入需要的参数。
                    services.AddTransient<ITest, Test>();
                }
            }
        }
        

        在单元测试类中,查找类名为 Startup 的类作为启动类。

      • 单元测试类的程序集 Startup 类。

        ///<summary>
        /// 程序集类。
        ///</summary>
        public class Startup { 
        
            ///<summary>
            /// 配置依赖注入的服务及生命周期(可选)。
            ///</summary>
            public void ConfigureServices(IServiceCollection services){
                // 注入需要的参数。
                services.AddTransient<ITest, Test>();
            }
        }
        
        ///<summary>
        /// 程序集类测试。
        ///</summary>
        public class NestedStartupTests(ITest test) {
        
            ///<summary>
            /// 测试。
            ///</summary>
            [Fact]
            public void Test() {
                Assert.True(test is Test);
            }
        }
        

        使用单元测试类的命名空间,或命名空间按照 “.” 逐步右截断查找名为 Startup 的类作为启动类。

      • 默认 XunitPlus.Startup 类。

        1. 在以上规则都没有设置的情况下生效。
        2. 使用 Inkslab.DI 实现的自动查找和实现依赖注入。
  • 便捷标记:可以 混合 使用,权重从上往下依次 递增

    • 使用 HeaderAttribute 添加请求头。
    • 使用 AuthorizationAttribute 添加认证请求头。
    • 继承 HttpContextAttribute 创建 HttpContext 请求上下文。
    • 继承 UserAttribute 创建请求上下文的用户信息。
    • 使用 PatternSeekAttribute 指定自动依赖注入扫描 DLL 文件的范围,如:Inkslab.*.dll
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.4 42 6/30/2024
1.2.3 74 6/20/2024
1.2.2 81 6/20/2024
1.2.1 134 5/17/2024
1.2.0 132 3/27/2024
1.1.0 97 3/27/2024
1.0.0 111 3/26/2024