我有一个Scheduler
类和一个使用unittest为__generate_potential_weeks
编写的测试
class Scheduler:
def __init__(self, num_teams, num_weeks):
self.potential_weeks = self.__generate_potential_weeks(num_teams)
# Other stuff
def __generate_potential_weeks(self, num_teams):
# Does some things
import unittest
from main import Scheduler
class SchedulerTestCase(unittest.TestCase):
def test_generate_weeks(self):
sch = Scheduler(4, 14)
weeks = sch.__generate_potential_weeks(4)
当我尝试测试__generate_potential_weeks
时,出现以下错误
AttributeError: 'Scheduler' object has no attribute '_SchedulerTestCase__generate_potential_weeks'
__generate_potential_weeks
__generate_potential_weeks
?它具有一些复杂的逻辑,我想与Scheduler
答案 0 :(得分:3)
双下划线在python中具有特殊含义。名称将被修改以避免在子类中出现名称冲突。
如果这不是您的意图,我鼓励您在单个下划线处进行标记。如果是这样,您仍然可以使用变形的名称来访问该函数。我认为这对您有用:sch._Scheduler__generate_potential_weeks(4)