View difference between Paste ID: MQKXNGRj and 7Bi3AAci
SHOW: | | - or go back to the newest paste.
1
#!/usr/bin/env python3
2
3
class Test():
4
5
    instances = []
6
7
    def __init__(self, name):
8
        self.name = name
9
        self.register(self)
10
        #vagy legyen inkább: self.__class__.register(self) ?
11
12
13
    @classmethod
14
    def register(cls,instance):
15
        cls.instances.append(instance)
16
17
    @classmethod
18
    def kill(cls,name):
19
        for instance in cls.instances:
20
            if instance.name == name:
21
                cls.instances.remove(instance)
22
23
    @classmethod
24
    def print_instances(cls):
25
        for instance in cls.instances:
26
            print(instance, instance.name)
27
            
28-
for name in ['Alpha', 'Beta', 'Gamma']:
28+
29-
    Test(name)
29+
import unittest
30
class TestTestCase(unittest.TestCase):
31-
Test.print_instances()
31+
    def test_kill(self):
32
        alpha, beta, gamma = [Test(name) for name in ['Alpha', 'Beta', 'Gamma']]
33-
Test.kill('Alpha')
33+
        self.assertSequenceEqual(Test.instances, (alpha, beta, gamma))
34
        Test.kill(alpha.name)
35-
Test.print_instances()
35+
        self.assertSequenceEqual(Test.instances, (beta, gamma))