Can I “fake” a package (or at least a module) in python for testing purposes?

Sure. Define a class, put the stuff you need inside that, assign the class to sys.modules[“classname”]. class fakemodule(object): @staticmethod def method(a, b): return a+b import sys sys.modules[“package.module”] = fakemodule You could also use a separate module (call it fakemodule.py): import fakemodule, sys sys.modules[“package.module”] = fakemodule

Django: How to create a model dynamically just for testing

You can put your tests in a tests/ subdirectory of the app (rather than a tests.py file), and include a tests/models.py with the test-only models. Then provide a test-running script (example) that includes your tests/ “app” in INSTALLED_APPS. (This doesn’t work when running app tests from a real project, which won’t have the tests app … Read more

Mocking USB input

Mocking usb devices using umockdev Umockdev is a linux based application which record the behaviour as well as properties of hardware and run the software independent of actual hardware it is running on. Hardware devices can be simulated in virtual environments without disturbing the whole system.It currently supports sysfs, uevents, basic support for /dev devices, … Read more

jmock mocking a static method

We don’t support mocking static methods in jMock because it doesn’t fit our design approach. We prefer not to use static methods for significant features that can affect the state of the system. We tend to use them just to support the OO code and make it more readable. That’s why we view mocking a … Read more

Mocking behaviour resets after each test with PowerMock

The method PowerMockito.mockStatic(…) invokes MockCreator.mock(…). This method regsiters a Runnable that will be executed after each test : MockRepository.addAfterMethodRunner(new MockitoStateCleaner()); This runnable cleans the internal state of Mockito : private static class MockitoStateCleaner implements Runnable { public void run() { clearMockProgress(); clearConfiguration(); } private void clearMockProgress() { clearThreadLocalIn(ThreadSafeMockingProgress.class); } private void clearConfiguration() { clearThreadLocalIn(GlobalConfiguration.class); } … Read more

How Moles Isolation framework is implemented?

Moles implements a CLR profiler (in particular the ICorProfilerCallback interface) that allows to rewrite MSIL method bodies before they are compiled into assembly code by the .NET runtime. This is done in particular through the JitCompileStarted callback. In each method, Moles introduces a detour that looks like this: static struct DateTime { static DateTime Now … Read more

python: mock a module

If you’re always accessing the variables in config.py like this: import config … config.VAR1 You can replace the config module imported by whatever module you’re actually trying to test. So, if you’re testing a module called foo, and it imports and uses config, you can say: from mock import patch import foo import config_test …. … Read more

Mocking using Moq in c#

Classic example which demonstrates that if you cannot unit test a particular component, REFACTOR it! This is why I love what any mocking framework enforces you to do – write decoupled code. In your example, the ProductBusiness class is tightly coupled with the ProductDataAccess class. You could decouple it using (like most of the answers … Read more