TLDR
my_funcを呼ぶ側がクラスと関係のない関数として呼び出そうとしているのが原因です。
また、貴方の呼びたい"my_func"の定義の仕方自体も少し間違っています。
クラス内の別の関数ということなのでインスタンスメソッドかクラスメソッドにしてしまいましょう。
python
1class MakeList(unittest.TestCase):
2
3 def setUp(self):
4 self.driver = webdriver.Remote(
5 command_executor='http://localhost:4444/wd/hub',
6 desired_capabilities=DesiredCapabilities.CHROME)
7
8 #こうする
9 def my_func_instance(self):
10 print("hello world : instance method")
11
12 #もしくはこうする
13 @classmethod
14 def my_func_class(cls):
15 print("hello world : class method")
16
17 #そしてこうする
18 def test_make_list(self):
19 #....
20 self.my_func_instance()
21 MakeList.my_func_class()
出力:
text
1hello world : instance method
2hello world : class method
細かく
test_make_list(self)はmy_funcをクラスの外部にある関数として呼び出そうとしています。
python
1class MakeList(unittest.TestCase):
2
3 def setUp(self):
4 self.driver = webdriver.Remote(
5 command_executor='http://localhost:4444/wd/hub',
6 desired_capabilities=DesiredCapabilities.CHROME)
7
8 def my_func(): # 呼ばれない (無視される)
9 print("hello world : Inside of the class ")
10
11 def test_make_list(self):
12 #....
13 my_func() # 自インスタンスや自クラスから呼んでいない -> じゃあクラスの外部にあるんだ!
14
15
16def my_func(): # ここにあれば呼べる
17 print("hello world : Outside of the class")
出力:
text
1hello world : Outside of the class
もし"my_func"がインスタンスの情報を触るのであれば、それはインスタンスメソッドとして定義してあげましょう。
以下のようにしましょう:
python
1def my_func(self): # 1. "self"を足す = インスタンスメソッド
2 #self.諸々 にアクセスできる
3 ...
4
5def test_make_list(self):
6 #....
7 self.my_func() # 2. 自インスタンスを頭につける (強制的に第一引数にインスタンスが入る)
もし"my_func"がクラスの情報だけを触るのであれば、それはクラスメソッドとして定義することもできます。
クラスメソッドは@classmethodデコレータで飾られている必要があります。
以下のようにしましょう:
python
1# 1. デコレータを与える = クラスメソッド
2@classmethod
3def my_func(cls): # 2. 第一引数として自分自身のクラスを取る
4 # cls.諸々 にアクセスできる = MakeList.諸々 と同義
5 # インスタンス変数 (self) にはアクセスできない
6 ...
7
8def test_make_list(self):
9 #....
10 MakeList.my_func() # 3. クラス名を頭につける (強制的に第一引数にクラスが入る)