Mocked value at method level not overriding class-level mock in unittest
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
Bug report
Bug description:
Description
I encountered an issue where mocking a value at the method level does not override the class-level mock in unit tests. Even though the method-level mock should take precedence, the value from the class-level mock is still being used.
Steps to Reproduce
Create a test class with a class-level mock using @mock.patch.
Add a method-level mock to one of the test methods to override the class-level mock.
Run the tests and observe that the class-level mock value is still used instead of the method-level mock.
Expected Behavior
The method-level mock should override the class-level mock, and the value set in the method-level mock should be used in the test.
Actual Behavior
The class-level mock value is used, and the method-level mock is ignored.
Sample code
import unittest
from unittest import mock
# Function to be mocked
def get_value():
return "original"
# Class-level mock
@mock.patch('module.get_value', return_value='class-level')
class TestMock(unittest.TestCase):
def test_class_level_mock(self, mock_get_value):
# This should return 'class-level'
result = get_value()
self.assertEqual(result, 'class-level')
@mock.patch('module.get_value', return_value='method-level')
def test_method_level_mock(self, mock_get_value):
# This should return 'method-level', but it returns 'class-level'
result = get_value()
self.assertEqual(result, 'method-level')
if __name__ == '__main__':
unittest.main()
CPython versions tested on:
3.10
Operating systems tested on:
No response
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、クラスレベルとメソッドレベルの両方に unittest.mock.patch を適用してサンプルを再現し、その後 unittest.mock.patch デコレーターと unittest.TestCase メソッドとの相互作用を調査します。メソッドレベルの戻り値が無視されるかどうかを確認し、期待されるメソッドレベルのオーバーライドを示す、対象を絞った回帰テストを追加または更新します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- testing-qa
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100