ПРИМЕЧАНИЯ:
- Я хочу запустить тест с pytest без использования команды стенда.
- Использование команды стендового запуска работает, но с использованием pytest нет.
- Выполнение тестов с помощью python -m unittest также дает ту же ошибку.
Код: Выделить всё
class FeedbackClassification(Document):
def validate(self):
self.validate_classification_code()
def validate_classification_code(self):
if frappe.db.exists(
"Feedback Classification", {"classification_code": self.classification_code, "name": ("!=", self.name)}
):
frappe.throw(
_("Duplicate classification codes {0} already exists").format(frappe.bold(self.classification_code)),
title=_("Duplicate Classification Code"),
exc=frappe.UniqueValidationError,
)
Код: Выделить всё
class TestFeedbackClassification(unittest.TestCase):
def setUp(self):
with patch.object(FeedbackClassification, "__init__", lambda x: None):
self.mock_feedback_classification = FeedbackClassification()
patcher = patch(
"frappe.throw", side_effect=lambda *args, **kwargs: (_ for _ in ()).throw(UniqueValidationError(args[0]))
)
self.mock_throw = patcher.start()
self.addCleanup(patcher.stop)
self.mock_feedback_classification.classification_code = None
self.mock_feedback_classification.name = "Test Classification"
def set_classification_code(self, classification_code):
self.mock_feedback_classification.classification_code = classification_code
@patch("frappe.db.exists")
def test_validate_duplicate_classification_code(self, mock_exists):
self.set_classification_code("Classification 1")
mock_exists.return_value = True
with self.assertRaises(UniqueValidationError) as cm:
self.mock_feedback_classification.validate()
expected_message = "Duplicate classification codes [b]Classification 1[/b] already exists"
self.assertIn(expected_message, str(cm.exception))
@patch("frappe.db.exists")
def test_validate_no_duplicate_classification_code(self, mock_exists):
self.set_classification_code("Classification")
mock_exists.return_value = False
try:
self.mock_feedback_classification.validate()
except UniqueValidationError:
self.fail("validate() raised UniqueValidationError unexpectedly!")
Код: Выделить всё
doctype/feedback_classification/test_feedback_classification.py::TestFeedbackClassification::test_validate_duplicate_classification_code failed: /usr/local/lib/python3.11/unittest/mock.py:1372: in patched
with self.decoration_helper(patched,
/usr/local/lib/python3.11/contextlib.py:137: in __enter__
return next(self.gen)
/usr/local/lib/python3.11/unittest/mock.py:1354: in decoration_helper
arg = exit_stack.enter_context(patching)
/usr/local/lib/python3.11/contextlib.py:505: in enter_context
result = _enter(cm)
/usr/local/lib/python3.11/unittest/mock.py:1443: in __enter__
original, local = self.get_original()
/usr/local/lib/python3.11/unittest/mock.py:1406: in get_original
original = target.__dict__[name]
frappe-bench/env/lib/python3.11/site-packages/werkzeug/local.py:311: in __get__
obj = instance._get_current_object()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
def _get_current_object() -> T:
try:
return get_name(local) # type: ignore[return-value]
except AttributeError:
> raise RuntimeError(unbound_message) from None
E RuntimeError: object is not bound
frappe-bench/env/lib/python3.11/site-packages/werkzeug/local.py:490: RuntimeError
- Выполнение теста с помощью стендовых тестов работает без любые проблемы.
- Проверка правильности применения исправлений.
- Тест должен пройти успешно с помощью pytest так же, как и при стендовых тестах.
Среда:
- Версия Frappe: 14.78.2
- Python версия: 3.11
- версия pytest: 8.3.2
Подробнее здесь: https://stackoverflow.com/questions/788 ... ith-pytest