The world of software development is ever-evolving, and with it, the tools and techniques we use to ensure clean, reliable, and efficient code. At Typemock, we’re committed to pushing the boundaries of what’s possible in unit testing. That’s why we’re excited to introduce the upcoming pure C++14 API for Isolator++.
This redesign marks a significant shift from macros to a modern, intuitive, and compile-time safe approach, ensuring that your testing experience is not just powerful but seamless. Let’s dive into what makes this new API a game-changer for C++ developers and why it’s a must-have for those aiming to streamline their unit testing with Typemock.
A New Era: From Macros to Pure C++14
One of the standout changes in the new API is our move away from macros. While macros have been a cornerstone of C++ for decades, they come with limitations that can hinder modern development practices. By embracing pure C++14, the new Isolator++ API offers several advantages:
- Compile-Time Safety: Many issues that previously surfaced during runtime are now caught at compilation. This reduces debugging time and ensures safer code.
- Modern Syntax: With a focus on clean and readable code, the API seamlessly integrates with modern C++ practices.
- Intuitive Design: The API eliminates the complexity of macros, offering a more natural and developer-friendly approach.
- Discoverability: All API calls start from the same class, making it easy to use IntelliSense and discover all available features effortlessly.
Automatic Cleanup with Local Isolator
In the old API, initiating and cleaning up the Isolator involved explicit calls to ISOLATOR_CLEANUP
. While functional, this approach required additional boilerplate code, which could lead to maintenance overhead.
In the new API, we’ve simplified the process by introducing the local Isolator. Here’s how it works:
- By adding a local Isolator instance to your test, cleanup becomes automatic—no extra steps required. This ensures that your tests remain clean, concise, and error-free.
Old Code:
1 2 3 4 5 6 7 8 9 10 11 |
protected: virtual void TearDown() { ISOLATOR_CLEANUP(); } TEST_F(MyTest, Example) { MyClass* fake = FAKE<MyClass>(); WHEN_CALLED(fake->Method()).Return(5); } |
New Code:
1 2 3 4 5 6 |
TEST_F(MyTest, Example) { Isolator a; auto fake = a.Fake.Instance<MyClass>(); a.CallTo(fake->Method()).WillReturn(5); } |
Cleaner and More Powerful C++ Mocking
Mocking with Isolator++ has always been powerful, but the new API takes it to the next level. Here’s how some common mocking tasks now look:
DoInstead Example:
One of the highlights of the new API is the DoInstead
feature, which allows you to replace method behavior with custom logic directly in your tests.
New Code:
1 |
a.CallTo(fake->CalculateSum()).WillDoInstead([&]() { return 42; }); |
This makes it easy to inject specific behavior without altering the original implementation.
Old Code:
1 |
WHEN_CALLED(fake->getSum()).Return(7); |
New Code:
1 |
a.CallTo(fake->getSum()).WillReturn(7); |
Private Calls? No Problem
Testing private methods and variables is also simplified. With the new API, you can mock private calls and manipulate variables with ease:
Old Code:
1 |
PRIVATE_WHEN_CALLED(fake, Init).Ignore(); |
New Code:
1 |
a.Private.CallTo(fake, "Init").WillBeIgnored(); |
The Future of C++ Testing
This new API isn’t just a set of features; it’s a philosophy. By aligning with modern C++ standards and focusing on developer experience, Isolator++ continues to lead the way in unit testing innovation. Whether you’re a seasoned developer or new to testing, this API makes mocking, testing, and verifying behavior faster, simpler, and more reliable.
Stay tuned as we finalize and release this exciting update. We can’t wait for you to experience the difference it makes in your development workflow.
Ready to simplify your unit testing? Let us know your thoughts on the new API or share your feedback in the comments below. Together, let’s build the future of clean, agile C++ code.