Beispiel #1
0
 /// <summary>
 /// 1:加载配置文件
 /// 2:初始依赖注入
 /// 3:初始模块数据
 /// </summary>
 public static void Run()
 {
     if (!_inited)
     {
         lock (LockObject)
         {
             if (!_inited)
             {
                 var stopwatch = new Stopwatch();
                 stopwatch.Start();
                 ContainerSingleton.SetContainer(new Container());
                 List <IBootStrategy> listStrategy = new List <IBootStrategy> {
                     new ConfigsLoad(),
                     new RegisterLoad(),
                     new ModulesLoad()
                 };
                 using (IEnumerator <IBootStrategy> enumerator = listStrategy.GetEnumerator())
                 {
                     while (enumerator.MoveNext())
                     {
                         enumerator.Current.Run();
                     }
                 }
                 stopwatch.Stop();
                 _inited = true;
             }
         }
     }
 }
Beispiel #2
0
        public async Task SetupAcceptanceTestBase()
        {
            // Create a new Lifetime Scope for the test: anything in the container with .AddScoped() will be resolved exactly once within each Scope
            _scope = ContainerSingleton.Instance().CreateScope();

            var testSettings = Resolve <TestSettings>();

            if (IsInProcessOnly && !testSettings.InProcess)
            {
                Assert.Inconclusive($"The test called {TestContext.FullyQualifiedTestClassName} can only be run InProcess; the current TestExecutionContext is not InProcess. Therefore, the test is being skipped. To avoid this message, consider using a TestCategory on the test and explicitly ignoring it in the Test Filter. ");
            }

            foreach (var key in testSettings.EnvironmentVariables.Keys)
            {
                Environment.SetEnvironmentVariable(key, testSettings.EnvironmentVariables[key], EnvironmentVariableTarget.Process);
            }

            Environment.SetEnvironmentVariable("ASPNETCORE_URLS", testSettings.BaseUrl, EnvironmentVariableTarget.Process);

            if (testSettings.InProcess)
            {
                _host = Program
                        .CreateHostBuilder()
                        .ConfigureServices(services =>
                {
                    ConfigureServices(services);
                })
                        .Build();

                await _host.StartAsync();
            }
        }
Beispiel #3
0
        public static void Initialize(TestContext testContext)
        {
            // Initialize this early on (for bootstrapping) and keep it simple: console output only (MsTest will capture this)
            Log.Logger = new LoggerConfiguration()
                         .Enrich
                         .FromLogContext()
                         .WriteTo
                         .Console()
                         .CreateLogger();

            // The Text Execution Context (environment, variables) are chosen in this order:
            //
            // 1. {PREFIX}TEST_EXECUTION_CONTEXT
            // 2. DEFAULT_TEST_EXECUTION_CONTEXT
            // 3. .runsettings is the fallback (if in the solution root)
            var testExecutionContextName = $"{Environment.GetEnvironmentVariable($"{PREFIX}TEST_EXECUTION_CONTEXT") ?? DEFAULT_TEST_EXECUTION_CONTEXT}";

            Log.Logger.Information($"Candidate Test Exection Context to use: {testExecutionContextName}");

            if (testContext.Properties.Contains(TEST_EXECUTION_CONTEXT_KEY_NAME))
            {
                Log.Logger.Information($"The .runsettings contains a property called {TEST_EXECUTION_CONTEXT_KEY_NAME}. We will retrieve that. ");
                testExecutionContextName = Convert.ToString(testContext.Properties[TEST_EXECUTION_CONTEXT_KEY_NAME]);
            }

            Log.Logger.Information($"TestExecutionContextName to use: {testExecutionContextName}");

            var testExecutionContextFilename = $"tec.{testExecutionContextName}.json";

            Log.Logger.Information($"TestExecutionContextFilename to be used: {testExecutionContextFilename}");

            var testExecutionContext = TestExecutionContextFactory.Create(testExecutionContextFilename);

            var environmentVariables = testExecutionContext.EnvironmentVariables;

            environmentVariables.Keys.ToList().ForEach(ev =>
            {
                Log.Logger.Information($"Setting Environment Variable '{ev}' to '{environmentVariables[ev]}' for this process. ");

                Environment.SetEnvironmentVariable(ev, environmentVariables[ev], EnvironmentVariableTarget.Process);
            });

            Log.Logger.Information("START: To initialize singleton container. ");
            ContainerSingleton.Initialize(Log.Logger, PREFIX, (prefix, services) =>
            {
                Log.Logger.Information($"    START: Callback before container is built. ");
                services.AddSingleton(testExecutionContext);
                Log.Logger.Information($"    END: Callback after container is built. ");
            });
            Log.Logger.Information("END: Initializing singleton container. ");

            _serviceProvider = ContainerSingleton.Instance;

            _testRunReporter = _serviceProvider.GetRequiredService <ITestRunReporter>();
            _testRunReporter.Setup();
        }
Beispiel #4
0
        public static void SetupTestRun(TestContext testContext)
        {
            // The Text Execution Context (environment, variables) are chosen in this order:
            //
            // 1. {Yeha.ConfigurationSingleton.ENVIRONMENT_VARIABLE_PREFIX}_TEST_EXECUTION_CONTEXT
            // 2. DEFAULT_TEST_EXECUTION_CONTEXT
            // 3. .runsettings is the fallback

            var testExecutionContext = $"{Environment.GetEnvironmentVariable($"{ConfigurationSingleton.ENVIRONMENT_VARIABLE_PREFIX}_TEST_EXECUTION_CONTEXT") ?? DEFAULT_TEST_EXECUTION_CONTEXT}";

            if (testContext.Properties.Contains(TEST_EXECUTION_CONTEXT_KEY_NAME))
            {
                testExecutionContext = Convert.ToString(testContext.Properties[TEST_EXECUTION_CONTEXT_KEY_NAME]);
            }

            var candidateTestExecutionContextFilename = $"testsettings.{testExecutionContext}.json";

            _instance = ContainerSingleton.InitializeContainer(candidateTestExecutionContextFilename);

            testContext.Properties.Add("ServiceProvider", _instance);
        }
Beispiel #5
0
 public static void Run()
 {
     ContainerSingleton.Initialize("A string");
 }