From 4378d2294910052f22434e70dcaeef97f8b761cc Mon Sep 17 00:00:00 2001
From: Andreas <andreasv@fsmpi.rwth-aachen.de>
Date: Wed, 18 Jul 2018 15:51:07 +0200
Subject: [PATCH] moved unit tests to own module

---
 .gitlab-ci.yml                             |  4 ++--
 README.md                                  |  4 ++--
 {tests => legacy_tests}/urlcheck_legacy.py |  0
 {tests => legacy_tests}/urlcheck_sinf.py   |  0
 runTests.py                                | 19 +++++++++++++++++++
 server.py                                  |  2 +-
 tests.py => tests/test_general.py          | 17 +++++------------
 tests/test_jobs.py                         |  2 ++
 8 files changed, 31 insertions(+), 17 deletions(-)
 rename {tests => legacy_tests}/urlcheck_legacy.py (100%)
 rename {tests => legacy_tests}/urlcheck_sinf.py (100%)
 create mode 100755 runTests.py
 rename tests.py => tests/test_general.py (98%)
 mode change 100755 => 100644
 create mode 100644 tests/test_jobs.py

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 597d82f..4a002f9 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -7,7 +7,7 @@ unittest:
     - python3 -V
     - uname -a
     - apt install -y sqlite3 locales-all git python3-flask python3-ldap3 python3-requests python3-lxml python3-icalendar python3-mysql.connector python3-requests python3-coverage
-    - python3 -m coverage run tests.py
+    - python3 -m coverage run runTests.py
     - python3 -m coverage report --include "./*"
     - python3 -m coverage report -m  --include "./*" > report.txt
     - python3 -m coverage html --include "./*"
@@ -33,4 +33,4 @@ deploy_staging:
     stage: deploy
     script:
     - pacman --noconfirm -Sy ansible git
-    
\ No newline at end of file
+    
diff --git a/README.md b/README.md
index 721b0ae..547684c 100644
--- a/README.md
+++ b/README.md
@@ -15,9 +15,9 @@ Hinweis: diese Variante startet eine lokale Testversion der Website, es sind nic
 Alternativ, insbesondere zum Testen der Zugriffsbeschränkungen: Siehe `nginx.example.conf`.
 
 ### Unittests
-Tests können mittels `./tests.py` ausgeführt werden.
+Tests können mittels `./runTests.py` ausgeführt werden.
 
-Coverage Tests können mittels `rm .coverage; python -m coverage run tests.py; python -m coverage html` ausgeführt werden. Dies erstellt einen Ordner `htmlcov` in dem HTML Output liegt.
+Coverage Tests können mittels `rm .coverage; python -m coverage run runTests.py; python -m coverage html` ausgeführt werden. Dies erstellt einen Ordner `htmlcov` in dem HTML Output liegt.
 
 ### Zum Mitmachen:
 1. Repo für den eigenen User forken, dafür den "Fork-Button" auf der Website verwenden
diff --git a/tests/urlcheck_legacy.py b/legacy_tests/urlcheck_legacy.py
similarity index 100%
rename from tests/urlcheck_legacy.py
rename to legacy_tests/urlcheck_legacy.py
diff --git a/tests/urlcheck_sinf.py b/legacy_tests/urlcheck_sinf.py
similarity index 100%
rename from tests/urlcheck_sinf.py
rename to legacy_tests/urlcheck_sinf.py
diff --git a/runTests.py b/runTests.py
new file mode 100755
index 0000000..b265725
--- /dev/null
+++ b/runTests.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python3
+import unittest
+import os
+import server
+
+def setUp():
+	server.app.testing = True
+
+def tearDown():
+	os.unlink(server.app.config['SQLITE_DB'])
+
+if __name__ == '__main__':
+	setUp()
+	try:
+		suite = unittest.defaultTestLoader.discover('./tests/', pattern="*")
+		unittest.TextTestRunner(verbosity=2, failfast=True).run(suite)
+	finally:
+		tearDown()
+
diff --git a/server.py b/server.py
index 2e54112..33cd960 100644
--- a/server.py
+++ b/server.py
@@ -26,7 +26,7 @@ if sys.argv[0].endswith('run.py'):
 	config['SQLITE_INIT_DATA'] = True
 	config['DEBUG'] = True
 config.from_pyfile('config.py', silent=True)
-if sys.argv[0].endswith('tests.py'):
+if sys.argv[0].endswith('runTests.py'):
 	print('running in test mode')
 	import tempfile
 	# ensure we always use a clean sqlite db for tests
diff --git a/tests.py b/tests/test_general.py
old mode 100755
new mode 100644
similarity index 98%
rename from tests.py
rename to tests/test_general.py
index 1106b57..2febe38
--- a/tests.py
+++ b/tests/test_general.py
@@ -1,17 +1,15 @@
-#!/usr/bin/env python3
-
 import os
 import unittest
-import server
 import json
-
+import server
 import flask
 from flask import url_for
 
+
 class VideoTestCase(unittest.TestCase):
-	@classmethod
-	def tearDownClass(cls):
-		os.unlink(server.app.config['SQLITE_DB'])
+	def tearDown(self):
+		pass
+		#os.unlink(server.app.config['SQLITE_DB'])
 	
 	def setUp(self):
 		server.app.testing = True
@@ -279,8 +277,3 @@ class VideoTestCase(unittest.TestCase):
 			self.login(c)
 			r = self.app.get('/internal/cutprogress')
 			assert r.status_code == 200
-
-
-if __name__ == '__main__':
-	unittest.main()
-
diff --git a/tests/test_jobs.py b/tests/test_jobs.py
new file mode 100644
index 0000000..245137d
--- /dev/null
+++ b/tests/test_jobs.py
@@ -0,0 +1,2 @@
+import os
+import unittest
-- 
GitLab