Пример #1
0
        private void ScanTypes(Type[] types)
        {
            foreach (var type in types)
            {
                var testFixtureAttrs = GetCustomAttributes <TestFixtureAttribute>(type);

                foreach (var testFixtureAttr in testFixtureAttrs)
                {
                    var concreteType = type;
                    if (type.ContainsGenericParameters)
                    {
                        concreteType = type.MakeGenericType(testFixtureAttr.TypeArgs);
                    }

                    var constructor = FindConstructor(concreteType, testFixtureAttr.Arguments);
                    object CreateTestObject() => constructor !.Invoke(testFixtureAttr.Arguments);

                    var testCases = new List <TestCase>();
                    var context   = new TestContext(concreteType, CreateTestObject);

                    foreach (var method in concreteType.GetMethods())
                    {
                        testCases.AddRange(ScanMethod(context, method));
                    }

                    if (testCases.Count == 0)
                    {
                        continue;
                    }

                    testCases.Sort((x, y) => x.Order - y.Order);
                    _testCases[context] = testCases;
                }
            }
        }
Пример #2
0
        private async Task <TestResult> RunTestCase(TestContext context, TestCase testCase)
        {
            var testObject = context.CreateTestObject();
            var method     = testCase.Method;
            var testResult = new TestResult(method);

            try
            {
                if (testObject is Node node)
                {
                    _sceneTree.Root.AddChild(node);
                }

                _setupMethods.InvokeAll(context, testObject);

                object obj = method.Invoke(testObject, testCase.Parameters);

                if (obj is IEnumerator coroutine)
                {
                    while (coroutine.MoveNext())
                    {
                        await Task.Delay(10);
                    }
                }

                testCase.ExpectedResult?.With(x => Assert.AreEqual(x, obj));
            }
            catch (Exception e)
            {
                testResult = new TestResult(method, e.InnerException ?? e);
            }
            finally
            {
                _teardownMethods.InvokeAll(context, testObject);

                (testObject as Node)?.QueueFree();
            }

            return(testResult);
        }
Пример #3
0
        private List <TestCase> ScanMethod(TestContext context, MethodInfo method)
        {
            var testCases = new List <TestCase>();

            if (HasCustomAttribute <SetUpAttribute>(method))
            {
                _setupMethods.AddMethod(context, method);
            }
            else if (HasCustomAttribute <TearDownAttribute>(method))
            {
                _teardownMethods.AddMethod(context, method);
            }
            else if (HasCustomAttribute <OneTimeSetUpAttribute>(method))
            {
                _oneTimeSetupMethods.AddMethod(context, method);
            }
            else if (HasCustomAttribute <OneTimeTearDownAttribute>(method))
            {
                _oneTimeTeardownMethods.AddMethod(context, method);
            }
            else
            {
                var testCaseAttrs = GetCustomAttributes <TestCaseAttribute>(method).ToList();

                if (testCaseAttrs.Count == 0)
                {
                    var testAttr = GetCustomAttribute <TestAttribute>(method);
                    if (testAttr != null)
                    {
                        var testCaseAttr = ToTestCaseAttribute(testAttr);
                        testCaseAttrs.Add(testCaseAttr);
                    }
                }

                foreach (var testCaseAttr in testCaseAttrs)
                {
                    if (testCaseAttr == null || testCaseAttr.Ignore != null || testCaseAttr.IgnoreReason != null)
                    {
                        continue;
                    }

                    var ignoreAttr = GetCustomAttribute <IgnoreAttribute>(method);
                    if (ignoreAttr != null)
                    {
                        var ignoreUntil = ignoreAttr.Until?.Map(DateTime.Parse);
                        if (ignoreUntil == null || DateTime.Now < ignoreUntil)
                        {
                            continue;
                        }
                    }

                    var repeatCount = GetCustomAttribute <RepeatAttribute>(method)?.GetCount() ?? 1;
                    var order       = GetCustomAttribute <OrderAttribute>(method)?.Order ?? int.MaxValue;

                    for (var i = 0; i < repeatCount; i++)
                    {
                        var testCase = new TestCase(method, testCaseAttr.Arguments, testCaseAttr.ExpectedResult, order);
                        testCases.Add(testCase);
                    }
                }
            }

            return(testCases);
        }