PyCharm - Edu - Unittest not running
Hi,
I created some exercises in PyCharm, added unittest, but when i simulate to be a student, PyCharms does not start the unit test. It does not give an error eaither. Any suggestions ?
Please sign in to leave a comment.
Hi, are you able to start a unittest from the terminal?
Please try this simple unittest code:
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()
Right click on the file and select "Run Python tests...".
from task import priemgetallen
class MyTestCase(unittest.TestCase):
def margeTest4(self):
top = 4
solution = [2, 3]
solution_to_test = priemgetallen(top)
self.assertEqual(priemgetallen(top).sort(), solution.sort(),
msg=f"Het resultaat voor priemgetallen({top}) is {solution}. Jouw oplossing geeft {solution_to_test}")
def margeTest5(self):
top = 5
solution = [2, 3, 5]
solution_to_test = priemgetallen(top)
self.assertEqual(priemgetallen(top).sort(), solution.sort(),
msg=f"Het resultaat voor priemgetallen({top}) is {solution}. Jouw oplossing geeft {solution_to_test}")
def margeTest6(self):
top = 6
solution = [2, 3, 5]
solution_to_test = priemgetallen(top)
self.assertEqual(priemgetallen(top).sort(), solution.sort(),
msg=f"Het resultaat voor priemgetallen({top}) is {solution}. Jouw oplossing geeft {solution_to_test}")
def margeTest7(self):
top = 7
solution = [2, 3, 5, 7]
solution_to_test = priemgetallen(top)
self.assertEqual(priemgetallen(top).sort(), solution.sort(),
msg=f"Het resultaat voor priemgetallen({top}) is {solution}. Jouw oplossing geeft {solution_to_test}")
def margeTest8(self):
top = 8
solution = [2, 3, 5, 7]
solution_to_test = priemgetallen(top)
self.assertEqual(priemgetallen(top).sort(), solution.sort(),
msg=f"Het resultaat voor priemgetallen({top}) is {solution}. Jouw oplossing geeft {solution_to_test}")
def margeTest9(self):
top = 9
solution = [2, 3, 5, 7]
solution_to_test = priemgetallen(top)
self.assertEqual(priemgetallen(top).sort(), solution.sort(),
msg=f"Het resultaat voor priemgetallen({top}) is {solution}. Jouw oplossing geeft {solution_to_test}")
def margeTest10(self):
top = 10
solution = [2, 3, 5, 7]
solution_to_test = priemgetallen(top)
self.assertEqual(priemgetallen(top).sort(), solution.sort(),
msg=f"Het resultaat voor priemgetallen({top}) is {solution}. Jouw oplossing geeft {solution_to_test}")
def margeTest11(self):
top = 11
solution = [2, 3, 5, 7, 11]
solution_to_test = priemgetallen(top)
self.assertEqual(priemgetallen(top).sort(), solution.sort(),
msg=f"Het resultaat voor priemgetallen({top}) is {solution}. Jouw oplossing geeft {solution_to_test}")
def margeTest12(self):
top = 12
solution = [2, 3, 5, 7, 11]
solution_to_test = priemgetallen(top)
self.assertEqual(priemgetallen(top).sort(), solution.sort(),
msg=f'Het resultaat voor priemgetallen({top}) is {solution}. Jouw oplossing geeft {solution_to_test}')
if __name__ == '__main__':
unittest.main()
solution = [2, 3]
for i in range(4, top + 1):
is_prime = False
halfi = int(i / 2) + 1
for j in range(2, halfi):
if i % j == 0:
is_prime = False
break
is_prime = True
if is_prime:
solution.append(i)
return solution
C:\Python310\python.exe "C:/Program Files/JetBrains/PyCharm Community Edition 2023.1/plugins/python-ce/helpers/pycharm/_jb_unittest_runner.py" --path "D:\xxxDropbox\xxx\___EduMa\Vakdidactiek Informatica\Module 07 - Programmeeromgevingen\Module7_B\KT Python\Priemgetallen\priemgetallenTest.py"
Testing started at 20:34 ...
Ran 0 tests in 0.000s
Launching unittests with arguments python -m unittest D:\xxxDropbox\xxx\___EduMa\Vakdidactiek Informatica\Module 07 - Programmeeromgevingen\Module7_B\KT Python\Priemgetallen\priemgetallenTest.py in D:\xxxDropbox\xxxs\___EduMa\Vakdidactiek Informatica\Module 07 - Programmeeromgevingen\Module7_B\KT Python\Priemgetallen
OK
Process finished with exit code 0
Empty suite
Found out that function in UNitTest must start with test_ te be able to be run. Now UnitTest run when started manually. But stil not from the course material :-(