Comments (5)
@fornellas I can try working on this within the next 2 weeks.
Internally I've already prepared a prototype of the possible fix and I need to reserve enough bandwidth to clean-it-up and fix tests as needed
from testslide.
For context - this was causing issues with a @property
that throws an exception in the unit-test environment, so somebody wants to mock it out. The current theory is that testslide is calling the property in order to find the original value so that it can un-patch it later, however everything explodes if the property raises an exception and there's no obvious workaround.
from testslide.
The current theory is that testslide is calling the property in order to find the original value so that it can un-patch it later, however everything explodes if the property raises an exception and there's no obvious workaround.
You'd be correct: this is how patching works. If the property is broken, then... things should break anyway :-/ There's no workaround: the property should be fixed.
self.mock_callable(host, "prop").to_return_value("patched")
This on the other hand... I'd expect to fail: a property is not... callable. It should have failed just as if one was trying to do mock_callable
against a string attribute of an object. This is a bug (though not the one reported here):
import testslide
class SampleClass:
def __init__(self):
self.attr = "str"
class SampleTest(testslide.TestCase):
def test_prop_callable(self) -> None:
host = SampleClass()
self.mock_callable(host, "attr").to_return_value("patched")
self.assertEqual(host.attr, "patched")
yields:
blah_test.SampleTest
test_prop_callable: ValueError: mock_callable() can only be used with callable attributes and 'str' is not.
from testslide.
If the property is broken, then... things should break anyway :-/ There's no workaround: the property should be fixed.
Property may call external service that is unavailable during tests and it will raise an error. This is what mocking should prevent.
from testslide.
Hum... I see your point, that's fair.
I think the way to address this is to add (yet) more complexity on top:
- When patching, check whether the attribute is a property.
- If it is, then save the property itself (to unpatch), and not the property value.
There are then 2 bugs to fix:
- Make the test below pass.
- Have mock_callable against a property / attribute fail.
import testslide
class SampleClass:
@property
def prop(self) -> str:
raise RuntimeError("must not be called")
class SampleTest(testslide.TestCase):
def test_prop_attr(self) -> None:
host = SampleClass()
self.patch_attribute(host, "prop", "patched")
self.assertEqual(host.prop, "patched")
from testslide.
Related Issues (20)
- Update documentation screeenshots & output examples HOT 2
- Add linter to ensure Copyright header on files
- BUG: Leaked task detection triggers for cancelled tasks HOT 2
- Context methods can't be async and are being quietly ignored
- StrictMock validations broken when used via inheritance.
- Improve Coroutine value error error message
- patch_attribute issue with Python 3.10 HOT 1
- Testslide runner not playing well with unittest.subtests HOT 1
- [RFC] [runner] Use Pattern.search() instead of Pattern.match() for "--filter-regex" behavior HOT 1
- Make Gitlab CI running tests/linting also for pull requests. HOT 1
- add assert_called_times(times:int) for callable mocks HOT 1
- `and_assert_not_called` shouldn't need any behaviour defined beforehand HOT 2
- Make to_return_values work for patch attribute HOT 1
- Cannot mock method with signature `def ...(...) -> Awaitable[...]` due to TypeCheckError
- Windows support HOT 1
- Cannot type check with `from __future__ import annotations`
- AttributeError for asyncio.coroutines.CoroWrapper in Python 3.11.0 HOT 1
- Test failures with Pygments 2.14
- Request: Container Matchers HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testslide.