1/*
2 * Copyright 2001-2008 Artima, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.scalatest.testng {
17
18  import org.scalatest._
19  import org.scalatest.TestRerunner
20  import org.scalatest.jmock._
21  import testng.testpackage._
22  import org.jmock.Mockery
23  import org.jmock.Expectations
24  import org.hamcrest.core.IsAnything
25  import org.scalatest.events._
26  import org.scalatest.mock.JMockCycle
27  import org.scalatest.mock.JMockCycleFixture
28  import org.scalatest.fixture.FixtureFunSuite
29
30  class TestNGSuiteSuite extends FixtureFunSuite with JMockCycleFixture with SuiteExpectations {
31
32    test("Reporter should be notified when test passes") { cycle => import cycle._
33
34      val reporter = mock[Reporter]
35
36      expecting { e =>
37        expectSingleTestToPass(e, reporter)
38      }
39
40      whenExecuting {
41        (new SuccessTestNGSuite()).runTestNG(reporter, new Tracker)
42      }
43    }
44
45    test("Reporter should be notified when test fails") { cycle => import cycle._
46
47      val reporter = mock[Reporter]
48
49      expecting { e =>
50        expectSingleTestToFail(e, reporter)
51      }
52
53      whenExecuting {
54        (new FailureTestNGSuite()).runTestNG(reporter, new Tracker)
55      }
56    }
57
58    test("If a test fails due to an exception, Report should have the exception") { () =>
59
60      val testReporter = new TestReporter
61
62      // when
63      (new FailureTestNGSuite()).runTestNG(testReporter, new Tracker)
64
65      // then
66      testReporter.lastEvent match {
67        case Some(TestFailed(_, _, _, _, _, throwable, _, _, _, _, _, _)) =>
68          assert(throwable.get.getMessage === "fail")
69        case _ => fail()
70      }
71    }
72
73    test("Report should be generated for each invocation") { cycle => import cycle._
74
75      val reporter = mock[Reporter]
76
77      // expect reporter gets 10 passing reports because invocationCount = 10
78      expecting { e =>
79        expectNTestsToPass(e, 10, reporter)
80      }
81
82      // when runnning the suite with method that has invocationCount = 10")
83      whenExecuting {
84        (new TestNGSuiteWithInvocationCount()).runTestNG(reporter, new Tracker)
85      }
86    }
87
88    test("Reporter should be notified when test is skipped") { cycle => import cycle._
89
90      val reporter = mock[Reporter]
91
92      // expect a single test should fail, followed by a single test being skipped
93      expecting { e => import e._
94        one(reporter).apply(`with`(new IsAnything[SuiteStarting]))
95        one(reporter).apply(`with`(new IsAnything[TestStarting]))
96        one(reporter).apply(`with`(new IsAnything[TestFailed]))
97        one(reporter).apply(`with`(new IsAnything[TestIgnored]))
98        one(reporter).apply(`with`(new IsAnything[SuiteCompleted]))
99      }
100
101      // when runnning the suite with a test that should fail and a test that should be skipped
102      whenExecuting {
103        (new SuiteWithSkippedTest()).runTestNG(reporter, new Tracker)
104      }
105    }
106
107    test("Only the correct method should be run when specifying a single method to run") { cycle => import cycle._
108
109      val reporter = mock[Reporter]
110
111      expecting { e =>
112        expectSingleTestToPass(e, reporter)
113      }
114
115      whenExecuting {
116        (new SuiteWithTwoTests()).runTestNG("testThatPasses", reporter, new Tracker)
117      }
118    }
119
120    test("Report for failing tests should include rerunner") { () =>
121
122      val testReporter = new TestReporter
123
124      // when - run the failing suite
125      new FailureTestNGSuite().runTestNG(testReporter, new Tracker)
126
127      // then get rerunnable from the event
128      testReporter.lastEvent match {
129        case Some(TestFailed(_, _, _, _, _, _, _, _, rerunnable, _, _, _)) =>
130          assert(rerunnable.isDefined)
131        case _ => fail()
132      }
133    }
134
135    test("Report for passing tests should include rerunner") { () =>
136
137      val testReporter = new TestReporter
138
139      // when - run the passing suite
140      new SuccessTestNGSuite().runTestNG(testReporter, new Tracker)
141
142      // then get rerunnable from report
143      val rerunner = testReporter.lastEvent.get.asInstanceOf[TestSucceeded].rerunner.get.asInstanceOf[TestRerunner];
144      // TODO we need a better assertion here
145    }
146
147
148    test("infoProvided should be available for BeforeMethod/Class/Suite annotations") { () =>
149      // this needs to be written after i figure out the mock integration
150    }
151
152    test("infoProvided should be available for AfterMethod/Class/Suite annotations") { () =>
153      // this needs to be written after i figure out the mock integration
154    }
155  }
156
157  package testpackage {
158
159    import org.testng.annotations._
160
161    class FailureTestNGSuite extends TestNGSuite {
162      @Test def testThatFails() { throw new Exception("fail") }
163    }
164
165    class SuccessTestNGSuite extends TestNGSuite {
166      @Test def testThatPasses() {}
167    }
168
169    class TestNGSuiteWithInvocationCount extends TestNGSuite {
170      @Test(invocationCount = 10) def testThatPassesTenTimes() {}
171    }
172
173    class SuiteWithSkippedTest extends TestNGSuite {
174      @Test(groups = Array("run")) def dependeeThatFails() { throw new Exception("fail") }
175      @Test(dependsOnGroups = Array("run")) def depender() {}
176    }
177
178    class SuiteWithTwoTests extends TestNGSuite {
179      @Test def testThatPasses() {}
180      @Test def anotherTestThatPasses() {}
181    }
182
183    class SuiteWithBeforeAndAfterAnnotations extends TestNGSuite {
184    }
185  }
186}
187
188
189