Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TestResult.cs « NUnitCore « src « nunit « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b0bfb7590d4848c3459b4ab069bdc9b605ba610 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
namespace NUnit.Framework {

  using System;
  using System.Collections;
  using System.Threading;

  /// <summary>A <c>TestResult</c> collects the results of executing
  /// a test case. It is an instance of the Collecting Parameter pattern.
  /// </summary><remarks>
  /// The test framework distinguishes between failures and errors.
  /// A failure is anticipated and checked for with assertions. Errors are
  /// unanticipated problems like an <c>ArgumentOutOfRangeException</c>.
  /// </remarks><seealso cref="ITest"/>
  public class TestResult {
    private ArrayList fFailures;
    private ArrayList fErrors;
    private ArrayList fListeners;
    private int fRunTests;
    private bool fStop;
    /// <summary>
    /// 
    /// </summary>
    public TestResult() {
      fFailures= new ArrayList();
      fErrors= new ArrayList();
      fListeners= new ArrayList();
    }

    /// <summary>Adds an error to the list of errors. The passed in exception
    /// caused the error.</summary>
    public void AddError(ITest test, Exception t) {
      lock(this) {
        fErrors.Add(new TestFailure(test, t));
        foreach (ITestListener l in CloneListeners()) {
          l.AddError(test, t);
        }
      }
    }

    /// <summary>Adds a failure to the list of failures. The passed in
    /// exception caused the failure.</summary>
    public void AddFailure(ITest test, AssertionFailedError t) {
      lock(this)
      {
        fFailures.Add(new TestFailure(test, t));
        foreach (ITestListener l in CloneListeners()) {
          l.AddFailure(test, t);
        }
      }
    }

    /// <summary>Registers a TestListener.</summary>
    public void AddListener(ITestListener listener) {
      lock(this)
        fListeners.Add(listener);
    }

    /// <summary>Returns a copy of the listeners.</summary>
    private ArrayList CloneListeners() {
      lock(this)
        return (ArrayList)fListeners.Clone();
    }

    /// <summary>Informs the result that a test was completed.</summary>
    public void EndTest(ITest test) {
      foreach (ITestListener l in CloneListeners()) {
        l.EndTest(test);
      }
    }

    /// <value>Gets the number of detected errors.</value>
    public int ErrorCount {
      get {return fErrors.Count; }
    }  

    /// <value>Returns an IEnumerable for the errors.</value>
    public IEnumerable Errors {
      get { return fErrors; }
    }

    /// <value>Gets the number of detected failures.</value>
    public int FailureCount {
      get {return fFailures.Count; }
    }

    /// <value>Returns an IEnumerable for the failures.</value>
    public IEnumerable Failures {
      get { return fFailures; }
    }

    /// <summary>Runs a TestCase.</summary>
    protected class ProtectedProtect: IProtectable {
      private TestCase fTest;
		/// <summary>
		/// 
		/// </summary>
		/// <param name="test"></param>
      public ProtectedProtect(TestCase test) {
        fTest = test;
      }
		/// <summary>
		/// 
		/// </summary>
      public void Protect() {
        fTest.RunBare();
      }
    }

    /// <summary>Unregisters a TestListener</summary>
    public void RemoveListener(ITestListener listener) {
      lock(this) {
        fListeners.Remove(listener);
      }
    }

	/// <summary>Runs a TestCase.</summary>
    internal void Run(TestCase test) {
      StartTest(test);
      IProtectable p = new ProtectedProtect(test);
      RunProtected(test, p);

      EndTest(test);
    }

    /// <value>Gets the number of run tests.</value>
    public int RunCount {
      get {return fRunTests; }
    }

    /// <summary>Runs a TestCase.</summary>
    public void RunProtected(ITest test, IProtectable p) {
      try {
        p.Protect();
      }
      catch (NUnitException e) {
        if (e.IsAssertionFailure)
          AddFailure(test, (AssertionFailedError)e.InnerException);
        else
          AddError(test, e.InnerException);
      }
      catch (ThreadAbortException e) { // don't catch by accident
        throw e;
      }
      catch (AssertionFailedError e) {
        AddFailure(test, e);
      }
      catch (System.Exception e) {
        AddError(test, e);
      }
    }

    /// <summary>Checks whether the test run should stop.</summary>
    public bool ShouldStop {
      get {return fStop; }
    }

    /// <summary>Informs the result that a test will be started.</summary>
    public void StartTest(ITest test) {
      int count = test.CountTestCases;
      lock(this)
        fRunTests += count;
      foreach (ITestListener l in CloneListeners()) {
        l.StartTest(test);
      }
    }

    /// <summary>Marks that the test run should stop.</summary>
    public void Stop() {
      fStop= true;
    }

    /// <value>Returns whether the entire test was successful or not.</value>
    public bool WasSuccessful {
      get {
        lock(this)
          return (FailureCount == 0) && (ErrorCount == 0);
      }
    }
  }
}