R包开发:mockery与stubthat包的差异咨询
Comparing
mockery and stubthat for R Package Stubbing Great question! I’ve used both packages extensively for speeding up R package tests and cutting down memory overhead, so let me break down their key differences based on hands-on experience:
Core API & Usability
mockery: Takes a flexible, low-level approach. You’ll work with explicitmock()objects, usestub()to swap out target functions, and leverage helpers likeexpect_called()orexpect_args()to verify exactly how dependencies were used. It’s perfect if you want fine-grained control over mock behavior and interaction tracking.stubthat: Follows a more opinionated, high-level philosophy. Its singlestub()function lets you replace a function with a fixed return value in one line—no need to predefine a mock object. This makes it super beginner-friendly for straightforward stubbing tasks where you just need to bypass expensive operations.
Feature Depth
mockery: Goes far beyond basic stubbing. You can:- Track call counts and inspect arguments passed to mocks
- Set up conditional return values (e.g., return 5 on the first call, 10 on the second)
- Make mocks throw errors to test error-handling logic
- Verify that a mock was never called
This makes it ideal for complex testing scenarios where you need to validate not just that a dependency was used, but how.
stubthat: Focuses on minimal, lightweight stubbing. It excels at replacing slow functions (like database queries or API calls) with static outputs to cut test time, but lacks advanced verification features. If you don’t need to check interactions, just replace the function, it’s a lean choice.
Community & Maintenance
mockery: As you noted, it’s been around longer and has a larger user base. You’ll find more tutorials, example code in popular R packages, and community support when troubleshooting. It’s also actively maintained, with regular updates to fix bugs and align with new R versions.stubthat: A newer, more niche alternative. While it’s stable and functional, the community is smaller, so you might find fewer resources for edge cases. Its smaller codebase is a plus if you want to minimize dependencies in your package.
Performance Overhead
mockery: Since it tracks detailed metadata (call logs, argument lists), it has a tiny bit more overhead compared tostubthat. For most test suites, this difference is unnoticeable, but if you’re stubbing hundreds of functions in a single run,stubthatmight be slightly more efficient in terms of memory and speed.- Both: Either way, using either package will drastically reduce test time and memory usage compared to running the original expensive functions—so you’re making a good choice either way.
Which Should You Choose?
- Pick
mockeryif: You need to verify interactions with dependencies, set up dynamic mock behavior, or work in a complex test suite that requires robust mock controls. - Pick
stubthatif: You just need to replace slow/memory-heavy functions with static values, and you prefer a simple, concise API without extra features.
Pro tip: You don’t have to pick just one! I’ve used both in the same test suite—
stubthatfor quick, simple stubs andmockeryfor more complex interaction verification. They play nicely together.
内容的提问来源于stack exchange,提问作者kjohnsen
相关产品推荐
相关产品推荐

