python / python/cpython

unittest mock with __new__ function lead to TypeError

Đang mở
#123,723 3 bình luận 2 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

import unittest
from unittest.mock import patch

class Summary(object):

    def __init__(self, path):
        print("Summary init")

    def __del__(self):
        print("Summary del")

    def get_summary(self):
        print("get_summary")

class MockSummary:
    def __init__(self, path):
        print("Mock start")

    def __del__(self):
        print("Mock end")

    def get_summary(self):
        print("mock_get_summary")

class TestSummary(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_summary(self):
        # 1 success case
        summary1 = Summary('path')
        summary1.get_summary()

        # 2 mock case
        mock = patch.object(Summary, '__new__', return_value=MockSummary('path'))
        mock.start()
        summary2 = Summary('path')
        summary2.get_summary()
        mock.stop()

        # 3 fail case
        summary3 = Summary('path')
        summary3.get_summary()

if __name__ == '__main__':
    unittest.main()

When run this python unittest file, it turns out with error:


Summary init
get_summary
Mock start
mock_get_summary
ESummary del

======================================================================
ERROR: test_summary (__main__.TestSummary.test_summary)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/admin/workspace/python/testSummaryAll.py", line 46, in test_summary
    summary3 = Summary('path')
               ^^^^^^^^^^^^^^^
TypeError: object.__new__() takes exactly one argument (the type to instantiate)

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)
Mock end

All the instance init after the mock stop will be failed.

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với bản tái hiện unittest được cung cấp và kiểm tra hành vi của unittest.mock.patch.object khi patch Summary.new. Tái hiện lỗi sau mock.stop(), sau đó theo dõi cách hành vi ban đầu của constructor được khôi phục. Công việc được hoàn tất khi có thể tạo các instance sau khi dừng patch và hồi quy được bao phủ bởi một test.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
testing-qa
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
38/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.