~nunit-core/nunitv2/2.5

« back to all changes in this revision

Viewing changes to src/NUnitFramework/core/TestSuite.cs

  • Committer: jnewkirk
  • Date: 2002-07-10 20:00:13 UTC
  • Revision ID: vcs-imports@canonical.com-20020710200013-j8us5mbi0usp7p02
initialĀ load

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Copyright (C) 2002. James W. Newkirk, Michael C. Two, Alexei A. Vorontsov. All Rights Reserved.
 
3
//
 
4
namespace Nunit.Core
 
5
{
 
6
        using System;
 
7
        using System.Collections;
 
8
        using System.Reflection;
 
9
        using Nunit.Framework;
 
10
 
 
11
        /// <summary>
 
12
        /// Summary description for TestSuite.
 
13
        /// </summary>
 
14
        public class TestSuite : Test
 
15
        {
 
16
                private ArrayList tests = new ArrayList();
 
17
 
 
18
                public TestSuite(string name) : base(name)
 
19
                {
 
20
                        ShouldRun = true;
 
21
                }
 
22
 
 
23
                protected internal virtual void Add(Test test) 
 
24
                {
 
25
                        if(test.ShouldRun)
 
26
                        {
 
27
                                test.ShouldRun = ShouldRun;
 
28
                                test.IgnoreReason = IgnoreReason;
 
29
                        }
 
30
                        tests.Add(test);
 
31
                }
 
32
 
 
33
                protected internal virtual TestSuite CreateNewSuite(string name)
 
34
                {
 
35
                        return new TestSuite(name);
 
36
                }
 
37
 
 
38
                public void Add(InvalidFixture fixture) 
 
39
                {
 
40
                        TestSuite testSuite = CreateNewSuite(fixture.OriginalType.Name);
 
41
                        Add(testSuite);
 
42
                        testSuite.ShouldRun=false;
 
43
                        testSuite.IgnoreReason = "Fixture is invalid";
 
44
 
 
45
                        MethodInfo [] methods = fixture.OriginalType.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.NonPublic);
 
46
                        foreach(MethodInfo method in methods) {
 
47
                                TestCase testCase = TestCaseBuilder.Make(fixture, method);
 
48
                                if(testCase != null) 
 
49
                                {
 
50
                                        testSuite.Add(testCase);
 
51
                                        testCase.ShouldRun = false;
 
52
                                        testCase.IgnoreReason = "Fixture is non-runnable";
 
53
                                }
 
54
                        }
 
55
 
 
56
                        return;
 
57
                }
 
58
 
 
59
                public void Add(object fixture) 
 
60
                {
 
61
                        TestSuite testSuite = CreateNewSuite(fixture.GetType().Name);
 
62
                        Add(testSuite);
 
63
 
 
64
                        Type ignoreMethodAttribute = typeof(Nunit.Framework.IgnoreAttribute);
 
65
                        object[] attributes = fixture.GetType().GetCustomAttributes(ignoreMethodAttribute, false);
 
66
                        if(attributes.Length == 1)
 
67
                        {
 
68
                                IgnoreAttribute attr = (IgnoreAttribute)attributes[0];
 
69
                                testSuite.ShouldRun = false;
 
70
                                testSuite.IgnoreReason = attr.Reason;
 
71
                        }
 
72
 
 
73
                        MethodInfo [] methods = fixture.GetType().GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.NonPublic);
 
74
                        foreach(MethodInfo method in methods)
 
75
                        {
 
76
                                TestCase testCase = TestCaseBuilder.Make(fixture, method);
 
77
                                if(testCase != null)
 
78
                                        testSuite.Add(testCase);
 
79
                        }
 
80
 
 
81
                        if(testSuite.CountTestCases == 0)
 
82
                        {
 
83
                                testSuite.ShouldRun = false;
 
84
                                testSuite.IgnoreReason = testSuite.Name + " does not have any tests";
 
85
                        }
 
86
                }
 
87
 
 
88
                public ArrayList Tests 
 
89
                {
 
90
                        get { return tests; }
 
91
                }
 
92
 
 
93
                public override int CountTestCases 
 
94
                {
 
95
                        get 
 
96
                        {
 
97
                                int count = 0;
 
98
 
 
99
                                foreach(Test test in Tests)
 
100
                                {
 
101
                                        count += test.CountTestCases;
 
102
                                }
 
103
                                return count;
 
104
                        }
 
105
                }
 
106
 
 
107
                public override TestResult Run(EventListener listener)
 
108
                {
 
109
                        TestSuiteResult suiteResult = new TestSuiteResult(this, Name);
 
110
 
 
111
                        listener.SuiteStarted(this);
 
112
 
 
113
                        suiteResult.Executed = true;
 
114
 
 
115
                        long startTime = DateTime.Now.Ticks;
 
116
                
 
117
                        RunAllTests(suiteResult,listener);
 
118
 
 
119
                        long stopTime = DateTime.Now.Ticks;
 
120
 
 
121
                        double time = ((double)(stopTime - startTime)) / (double)TimeSpan.TicksPerSecond;
 
122
 
 
123
                        suiteResult.Time = time;
 
124
                        if(!ShouldRun) suiteResult.NotRun(this.IgnoreReason);
 
125
 
 
126
                        listener.SuiteFinished(suiteResult);
 
127
 
 
128
                        return suiteResult;
 
129
                }
 
130
 
 
131
                protected virtual void RunAllTests(TestSuiteResult suiteResult,EventListener listener)
 
132
                {
 
133
                        foreach(Test test in Tests)
 
134
                        {
 
135
                                suiteResult.AddResult(test.Run(listener));
 
136
                        }
 
137
                }
 
138
        }
 
139
}