Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Pontesegger2017-11-02 14:22:42 +0000
committerChristian Pontesegger2017-11-02 14:22:42 +0000
commit4c6677131d7bb6fb70781bb2d5a37d03b3da2a18 (patch)
treeafb8cc566715c4f55305fe4eccf87aaa1ce40616
parentc5b79604d08fdf4ae3084a6ee126e4727c196839 (diff)
downloadorg.eclipse.ease.scripts-4c6677131d7bb6fb70781bb2d5a37d03b3da2a18.tar.gz
org.eclipse.ease.scripts-4c6677131d7bb6fb70781bb2d5a37d03b3da2a18.tar.xz
org.eclipse.ease.scripts-4c6677131d7bb6fb70781bb2d5a37d03b3da2a18.zip
Updated Unit testing tutorial
-rw-r--r--JavaScript Unit Testing Tutorial/Testsuite.suite21
-rw-r--r--Unit Testing Tutorial/.project (renamed from JavaScript Unit Testing Tutorial/.project)2
-rw-r--r--Unit Testing Tutorial/01 JavaScript/01 Valid tests.js (renamed from JavaScript Unit Testing Tutorial/Tests/Simple/01 Valid tests.js)2
-rw-r--r--Unit Testing Tutorial/01 JavaScript/02 Test errors.js (renamed from JavaScript Unit Testing Tutorial/Tests/Simple/02 Test errors.js)10
-rw-r--r--Unit Testing Tutorial/01 JavaScript/03 Long Running Tests.js (renamed from JavaScript Unit Testing Tutorial/Tests/Advanced/01 Long Running Tests.js)2
-rw-r--r--Unit Testing Tutorial/01 JavaScript/04 No test cases.js (renamed from JavaScript Unit Testing Tutorial/Tests/Advanced/02 No test cases.js)2
-rw-r--r--Unit Testing Tutorial/01 JavaScript/05 Test Metadata.js (renamed from JavaScript Unit Testing Tutorial/Tests/Advanced/03 Test Metadata.js)2
-rw-r--r--Unit Testing Tutorial/01 JavaScript/06 Variables.js (renamed from JavaScript Unit Testing Tutorial/Tests/Advanced/04 Variables.js)4
-rw-r--r--Unit Testing Tutorial/01 JavaScript/07 Disabled tests.js19
-rw-r--r--Unit Testing Tutorial/01 JavaScript/08 Test Classes.js143
-rw-r--r--Unit Testing Tutorial/02 Python/01 Simple tests.py37
-rw-r--r--Unit Testing Tutorial/02 Python/02 PyUnit tests.py39
-rw-r--r--Unit Testing Tutorial/Testsuite.suite14
13 files changed, 270 insertions, 27 deletions
diff --git a/JavaScript Unit Testing Tutorial/Testsuite.suite b/JavaScript Unit Testing Tutorial/Testsuite.suite
deleted file mode 100644
index 07691d7..0000000
--- a/JavaScript Unit Testing Tutorial/Testsuite.suite
+++ /dev/null
@@ -1,21 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<testsuite>
-<testfiles>
-<testfile>project://Tests/Simple/02 Test errors.js</testfile>
-<testfile>project://Tests/Advanced/01 Long Running Tests.js</testfile>
-<testfile>project://Tests/Simple/01 Valid tests.js</testfile>
-<testfile>project://Tests/Advanced/02 No test cases.js</testfile>
-<testfile>project://Tests/Advanced/04 Variables.js</testfile>
-<testfile>project://Tests/Advanced/03 Test Metadata.js</testfile>
-</testfiles>
-<variables>
-<variable description="Amount of test loops for Tests/Advanced/04 Variables." name="testCounter">5</variable>
-<variable description="Identifier of test hardware" name="testDevice">&quot;Magic Hardware&quot;</variable>
-</variables>
-<codeFragments>
-<codeFragment name="TestSuite Teardown">print(&quot;Test suite finished.&quot;);</codeFragment>
-<codeFragment name="TestSuite Setup">print(&quot;Test suite started...&quot;);</codeFragment>
-</codeFragments>
-<flags/>
-<description></description>
-</testsuite> \ No newline at end of file
diff --git a/JavaScript Unit Testing Tutorial/.project b/Unit Testing Tutorial/.project
index 69fa1c1..34aa23b 100644
--- a/JavaScript Unit Testing Tutorial/.project
+++ b/Unit Testing Tutorial/.project
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
- <name>JavaScript Unit Testing Tutorial</name>
+ <name>Unit Testing Tutorial</name>
<comment></comment>
<projects>
</projects>
diff --git a/JavaScript Unit Testing Tutorial/Tests/Simple/01 Valid tests.js b/Unit Testing Tutorial/01 JavaScript/01 Valid tests.js
index 85708a7..8fd79a5 100644
--- a/JavaScript Unit Testing Tutorial/Tests/Simple/01 Valid tests.js
+++ b/Unit Testing Tutorial/01 JavaScript/01 Valid tests.js
@@ -9,6 +9,8 @@
* Christian Pontesegger - initial API and implementation
*******************************************************************************/
+loadModule("/Unittest", false);
+
// starts a simple test
startTest("empty", "an empty test case");
// ends a test
diff --git a/JavaScript Unit Testing Tutorial/Tests/Simple/02 Test errors.js b/Unit Testing Tutorial/01 JavaScript/02 Test errors.js
index 8340d73..c7ba981 100644
--- a/JavaScript Unit Testing Tutorial/Tests/Simple/02 Test errors.js
+++ b/Unit Testing Tutorial/01 JavaScript/02 Test errors.js
@@ -9,6 +9,8 @@
* Christian Pontesegger - initial API and implementation
*******************************************************************************/
+loadModule("/Unittest", false);
+
// test containing assertions that fail
startTest("invalid assertions", "a test containing invalid assertions");
assertTrue(true);
@@ -17,14 +19,14 @@ assertFalse(true);
assertFalse(false);
endTest();
-// manually create a failure
+// manually create an error
startTest("failure", "test throwing a failure");
-failure("test broken, stop execution here");
+error("test broken, stop execution here");
-// an exception would also create a failure
+// an exception would also create an error
throw new java.lang.Exception("code exception");
endTest();
startTest("never to be reached");
-// not being reached as the failure above terminates test file execution
+// not being reached as the error above terminates test file execution
endTest();
diff --git a/JavaScript Unit Testing Tutorial/Tests/Advanced/01 Long Running Tests.js b/Unit Testing Tutorial/01 JavaScript/03 Long Running Tests.js
index 40c9a74..60813eb 100644
--- a/JavaScript Unit Testing Tutorial/Tests/Advanced/01 Long Running Tests.js
+++ b/Unit Testing Tutorial/01 JavaScript/03 Long Running Tests.js
@@ -9,6 +9,8 @@
* Christian Pontesegger - initial API and implementation
*******************************************************************************/
+loadModule("/Unittest", false);
+
function sleep(milliSeconds) {
end = java.lang.System.currentTimeMillis() + milliSeconds;
while (java.lang.System.currentTimeMillis() < end)
diff --git a/JavaScript Unit Testing Tutorial/Tests/Advanced/02 No test cases.js b/Unit Testing Tutorial/01 JavaScript/04 No test cases.js
index 4515e48..4c6796e 100644
--- a/JavaScript Unit Testing Tutorial/Tests/Advanced/02 No test cases.js
+++ b/Unit Testing Tutorial/01 JavaScript/04 No test cases.js
@@ -9,6 +9,8 @@
* Christian Pontesegger - initial API and implementation
*******************************************************************************/
+loadModule("/Unittest", false);
+
// you are not required to use test cases; they simply help to structure your test results
assertEquals(3, 2+1);
diff --git a/JavaScript Unit Testing Tutorial/Tests/Advanced/03 Test Metadata.js b/Unit Testing Tutorial/01 JavaScript/05 Test Metadata.js
index 40567cc..952c612 100644
--- a/JavaScript Unit Testing Tutorial/Tests/Advanced/03 Test Metadata.js
+++ b/Unit Testing Tutorial/01 JavaScript/05 Test Metadata.js
@@ -9,6 +9,8 @@
* Christian Pontesegger - initial API and implementation
*******************************************************************************/
+loadModule("/Unittest", false);
+
addMetaData("author", "Christian Pontesegger");
addMetaData("license", "EPL v1.0");
diff --git a/JavaScript Unit Testing Tutorial/Tests/Advanced/04 Variables.js b/Unit Testing Tutorial/01 JavaScript/06 Variables.js
index d6677ab..eb3c78a 100644
--- a/JavaScript Unit Testing Tutorial/Tests/Advanced/04 Variables.js
+++ b/Unit Testing Tutorial/01 JavaScript/06 Variables.js
@@ -9,10 +9,12 @@
* Christian Pontesegger - initial API and implementation
*******************************************************************************/
+loadModule("/Unittest", false);
+
print("Running tests on " + testDevice);
// loop over a test parameter
-for ( var cycle = 1; cycle <= testCounter; cycle++) {
+for ( var cycle = 1; cycle <= parseInt(testCounter); cycle++) {
startTest("cycle " + cycle, "valid test");
endTest();
}
diff --git a/Unit Testing Tutorial/01 JavaScript/07 Disabled tests.js b/Unit Testing Tutorial/01 JavaScript/07 Disabled tests.js
new file mode 100644
index 0000000..112b3d6
--- /dev/null
+++ b/Unit Testing Tutorial/01 JavaScript/07 Disabled tests.js
@@ -0,0 +1,19 @@
+/*******************************************************************************
+ * Copyright (c) 2017 Christian Pontesegger and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Christian Pontesegger - initial API and implementation
+ *******************************************************************************/
+
+loadModule("/Unittest", false);
+
+startTest("active", "an empty test case");
+endTest();
+
+startTest("disabled", "test disabled");
+ignoreTest("Dynamically disabled test");
+endTest();
diff --git a/Unit Testing Tutorial/01 JavaScript/08 Test Classes.js b/Unit Testing Tutorial/01 JavaScript/08 Test Classes.js
new file mode 100644
index 0000000..121b2a9
--- /dev/null
+++ b/Unit Testing Tutorial/01 JavaScript/08 Test Classes.js
@@ -0,0 +1,143 @@
+/*******************************************************************************
+ * Copyright (c) 2017 Christian Pontesegger and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Christian Pontesegger - initial API and implementation
+ *******************************************************************************/
+
+loadModule("/Unittest");
+
+function sleep(milliSeconds) {
+ stop = java.lang.System.currentTimeMillis() + milliSeconds;
+ while (java.lang.System.currentTimeMillis() < stop)
+ ;
+}
+
+/**
+ * Create instances containing test functions.
+ */
+var modernTest = {
+
+ // special marker to indicate this instance as unit test
+ __unittest : '',
+
+ // unit test description
+ __description : 'Test class demonstrating modern testing',
+
+ // globally ignore this instance and all its test methods
+ // __ignore : 'tests ignored -> list ignored testcases',
+
+ /**
+ * Method to be called before each test function. The name is not important, instead we are looking for our custom annotations
+ */
+ setup : function() {
+ // annotation indicating that this is the test setup method
+ '@before';
+
+ print("this is the test setup")
+ },
+
+ /**
+ * Method to be called after each test, even in case of errors.
+ */
+ teardown : function() {
+ '@after';
+
+ print("this is the test teardown")
+ },
+
+ /**
+ * Method to be called once before all tests if this instance get executed.
+ */
+ setupClass : function() {
+ '@beforeclass';
+
+ print("this is the testclass setup")
+ },
+
+ /**
+ * Method to be called once after all tests of this instance got executed.
+ */
+ teardownClass : function() {
+ '@afterclass';
+
+ print("this is the testclass teardown")
+ },
+
+ /**
+ * Simple test case. The @test marker indicates it as a test.
+ */
+ valid : function() {
+ '@test';
+ '@description(Working testcase)';
+
+ assertTrue(true);
+ },
+
+ /**
+ * Instead of an @test annotation we could prefix the function name with 'test'.
+ */
+ testInvalid : function() {
+ '@description(testcase that fails)';
+
+ assertTrue(false);
+ },
+
+ /**
+ * Even after throwing further tests of this instance will get executed.
+ */
+ testError : function() {
+ '@description(testcase throwing an exception)';
+
+ throw "this does not work";
+ },
+
+ testError2 : function() {
+ '@description(testcase throwing an exception)';
+
+ java.lang.Class.forName("NonExistingClass");
+ },
+
+ /**
+ * Expect a java exception to be thrown. Test is pass when exception is detected.
+ */
+ testExpectError : function() {
+ '@description(testcase expecting an exception)';
+ '@expect(java.lang.ClassNotFoundException)';
+
+ java.lang.Class.forName("NonExistingClass");
+ },
+
+ /**
+ * This test will fail as the expected exception is not thrown.
+ */
+ testExpectErrorButIsOK : function() {
+ '@description(testcase expecting an exception)';
+ '@expect(java.lang.ClassNotFoundException)';
+
+ },
+
+ /**
+ * Disable a test.
+ */
+ testIgnored : function() {
+ '@description(ignored testcase)';
+ '@ignore(manually disabled testcase)';
+
+ assertTrue(false);
+ },
+
+ /**
+ * Test failing due to exceeded execution time.
+ */
+ testTimeout : function() {
+ '@description(test running in a timeout)';
+ '@timeout(100)';
+
+ sleep(150);
+ },
+}
diff --git a/Unit Testing Tutorial/02 Python/01 Simple tests.py b/Unit Testing Tutorial/02 Python/01 Simple tests.py
new file mode 100644
index 0000000..5cc6e5d
--- /dev/null
+++ b/Unit Testing Tutorial/02 Python/01 Simple tests.py
@@ -0,0 +1,37 @@
+###############################################################################
+# Copyright (c) 2017 Christian Pontesegger and others.
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Contributors:
+# Christian Pontesegger - initial API and implementation
+###############################################################################
+
+loadModule("/Unittest");
+
+startTest("valid", "simple valid test")
+assertTrue(True)
+endTest()
+
+startTest("invalid", "simple fail test")
+assertTrue(False)
+endTest()
+
+startTest("ignored", "do not run test")
+ignoreTest("will ignore failures, but will still report errors")
+assertTrue(False)
+endTest();
+
+startTest("timeout", "see if a test takes too long")
+setTestTimeout(100);
+start = java.lang.System.currentTimeMillis()
+while (start + 200 > java.lang.System.currentTimeMillis()):
+ pass
+
+endTest();
+
+startTest("error", "test raising an error")
+# raise(BaseException("does not work"))
+endTest() \ No newline at end of file
diff --git a/Unit Testing Tutorial/02 Python/02 PyUnit tests.py b/Unit Testing Tutorial/02 Python/02 PyUnit tests.py
new file mode 100644
index 0000000..641f9b7
--- /dev/null
+++ b/Unit Testing Tutorial/02 Python/02 PyUnit tests.py
@@ -0,0 +1,39 @@
+import unittest
+
+class Test(unittest.TestCase):
+
+ def setUp(self):
+ print("\t\ttest setup")
+ pass
+
+ def tearDown(self):
+ print("\t\ttest teardown")
+ pass
+
+ def testValid(self):
+ self.assertTrue(True)
+ pass
+
+ def testInvalid(self):
+ self.assertTrue(False)
+ pass
+
+ def testError(self):
+ raise(BaseException("does not work"))
+
+ @unittest.expectedFailure
+ def testExpectError(self):
+ raise(BaseException("expected exception"))
+
+ @unittest.expectedFailure
+ def testExpectErrorButIsOK(self):
+ pass
+
+ @unittest.skip("demonstrating skipping")
+ def testIgnored(self):
+ self.assertTrue(False)
+ pass
+
+if __name__ == "__main__":
+ print("running from python directly")
+ unittest.main() \ No newline at end of file
diff --git a/Unit Testing Tutorial/Testsuite.suite b/Unit Testing Tutorial/Testsuite.suite
new file mode 100644
index 0000000..1df1ea0
--- /dev/null
+++ b/Unit Testing Tutorial/Testsuite.suite
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="ASCII"?>
+<TestSuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://eclipse.org/ease/unittest/testsuite/1.0" version="1.0">
+ <includeFilter>project://**.js&#xD;
+project://**.py</includeFilter>
+ <variable fullName="/testDevice">
+ <content>my basic controller</content>
+ <description>The name of the test device these tests are running on</description>
+ </variable>
+ <variable fullName="/testCounter">
+ <content>5</content>
+ <description>Amount of test cycles to execute</description>
+ </variable>
+ <flag key="PREFERRED_ENGINE_ID" value="org.eclipse.ease.javascript.rhinoDebugger"/>
+</TestSuite>

Back to the top