Testing Tips for When Unit Testing Is Too Hard

Testing Tips for When Unit Testing Is Too Hard

echnologies such as the xUnit framework, combined with Maven, BuildBot, or CruiseControl, enable unit testing under continuous integration, which gives developer productivity a huge boost. However, writing true unit tests even with these tools sometimes is just too difficult. When faced with tightly coupled code, test-averse legacy code, and buggy libraries, developers end up writing tests that aren’t quite up to the unit-test standard. Instead, they compromise by running tests that cover several functionalities at once, opening a few sockets, connecting to databases, reading or writing files, and basically abandoning pure unit testing.

This article describes the frustrating scenarios that lead developers to take these shortcuts, and it provides useful tips for dealing with them.

This Code is Tightly Coupled!
Unfortunately, not all software adheres to the best practice of loose coupling, a principle that greatly eases unit testing. For instance, domain objects sometimes are tightly linked to their underlying storage, such as a database. In these scenarios, you can’t feasibly test the code without a database. If you tried, it would require refactoring the entire code base?a job that may take years.

However, handling software that violates one important principle does not mean you have to dismiss unit testing all together. You can still write useful tests (called integration tests) on tightly coupled code, even though a failed integration test will not indicate the exact cause of the problem. To conduct effective integration tests, you just need to keep the testing environment minimalist, isolated, and constant:

  • Keeping the testing environment minimalist has clear benefits. For example, if the code you’re testing is tightly bound to a database, there is no need to populate the database with every single test example you can imagine. In fact, you should try to keep things as simple as possible, so as to minimize the amount of investigation time when tests fail. Testing code that is tightly bound to databases also requires you to deal with a lot of different databases for different test suites. If you have to read files, consider applying the minimalist principle by creating a lot of small files?one per test if possible.
  • Keeping the testing environment isolated has obvious benefits as well, yet running two test suites simultaneously on a single database is a common practice. This leads to tests failing because the other processes that are running in the same environment change something. You must isolate testing environments as much as possible. One solution is using virtual machines. You can put several of them on the same hardware, while keeping them reasonably isolated from each other.
  • Keeping the testing environment constant allows you to rerun tests under the same conditions. If you conduct your tests under continuous integration (and you should), you re-run failed tests so you can debug them easily. So try to keep your environment clean: if you need the file system, unzip a new version of the necessary folders before the tests; if you need a database, reload it prior to testing; if you need the system time, consider using virtual machines with fooled clocks.

This Language Has No Good Testing Framework!
Once upon a time, languages weren’t designed to host test libraries. Developers were supposed to write good code right from the start. Languages have evolved since then, but you still have to handle huge code bases in these older languages, which definitely would benefit from unit testing. The most basic way to deal with such languages is to write a large number of small programs, each of them representing a test. Then you can encapsulate and run these programs within a new language that has a testing framework. This is certainly not a very elegant solution, but it works.

A more advanced approach is creating entries in your tested software that you could access with an external language. If your tested program is developed with C/C++, for example, you could create entry points and access them through Python (SWIG anyone?). If your old language creates a Windows DLL, you could use quite a few languages and frameworks to test the DLL entry points. The drawback to this solution is that you add non-functional code to your software, and the added software could itself contain a bug. But adding it will probably help you refactor your code.

There Are Bugs in These Underlying Libraries!
Sometimes your unit tests pass on nearly every platform, but the application encounters bugs on a specific version of an OS. The reason may be that annoying bugs exist in a library on this platform. Because you followed the library specifications and simulated the library with mock objects, you did not see the bugs within your unit tests. If you can’t fix the library itself, you can employ two strategies to deal with this:

  • Consider your unit tests incorrect because they don’t cover all test cases. In this strategy, you redesign some of the mock objects to simulate the library bugs and then cover this behavior in your unit tests. I personally disapprove of this option, however. Unit tests can be considered specifications, and writing unit tests to describe a library bug is like specifying a bug for subsequent generations of the code.
  • Consider your unit tests correct (and they are, considering the specification of the library), but create integration tests to properly test your application anyway. The integration tests integrate several pieces of the application code together to have them tested against the actual environment (file system, database, network, and so on). Some of these tests will fail, and you will have to refactor the code to circumvent the faulty library behavior.

In either case, creating integration tests is always a good thing. They test your code in a more realistic situation than pure unit tests. Unit tests work with expected behavior of small and isolated pieces of code, whereas integration tests put the code in real situations, such as accessing a real database instead of mock objects. Once you begin using them, you will soon find yourself wanting to automate and manage them (continuous integration again). But automating integration tests is usually a pain because it requires much more context handling, such as recreating a clean database before you run them. Unit tests are obviously much simpler to automate.

Integration Tests?Unit Tests “Lite”
Realistically, you will face situations when you can’t create unit tests at a reasonable cost. This unfortunately is the case with tightly coupled code, test-averse legacy code, and buggy libraries. If you want to introduce true unit tests in the long term, you can definitely make a good continuous integration system out of (less unit-like) integration tests.

devx-admin

devx-admin

Share the Post:
USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India,

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies in the USA. Through a

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

Build a Website

How Much Does It Cost to Build a Website?

Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted