Reader small image

You're reading from  Building Microservices with .NET Core

Product typeBook
Published inJun 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781785887833
Edition1st Edition
Languages
Right arrow
Authors (3):
Gaurav Aroraa
Gaurav Aroraa
author image
Gaurav Aroraa

Gaurav Aroraa has done M.Phil in computer science. He is a Microsoft MVP, life time member of Computer Society of India (CSI), certified as a scrum trainer/coach, XEN for ITIL-F and APMG for PRINCE-F and PRINCE-P. Gaurav serves as a mentor at IndiaMentor, open source developer, contributor to TechNet Wiki co-founder of Innatus Curo Software LLC. In 19+ years of his career, he has mentored thousands of students and industry professionals. You can tweet Gaurav on his twitter handle @g_arora
Read more about Gaurav Aroraa

Lalit Kale
Lalit Kale
author image
Lalit Kale

Lalit Kale is a technical architect and consultant with more than 12 years of industry experience. Lalit has helped clients achieve tangible business outcomes through the implementation of best practices in software development. He is a practitioner of TDD and DDD, and a big believer in agile and lean methodologies. He has worked with several organizations, from start-ups to large enterprises, in making their systems successful, be it in-house or mission critical, with clients in the USA, the UK, Germany, Ireland, and India. His current interests include container technologies and machine learning using Python. He holds a bachelor's degree in engineering (IT).
Read more about Lalit Kale

Manish Kanwar
Manish Kanwar
author image
Manish Kanwar

Manish Kanwar completed his masters of science in computer applications from MD University, India, and is a cofounder of Innatus Curo Software LLC, with a presence in India. He has been working in the IT industry across domains for the last 17 years. He started exploring .NET right from the first release and has been glued to it ever since. His range of experience includes global wealth management (financial service industry, USA), life insurance (insurance industry, USA), and document management system (DMS), ECMS, India. Manish does his bit for the community by helping young professionals through the IndiaMentor platform.
Read more about Manish Kanwar

View More author details
Right arrow

Chapter 4. Testing Strategies

Quality assurance or testing is a great way to assess a system, program, or an application with different aspects. Sometimes, a system requires testing to identify erroneous code; on other occasions, we may need it to assess our system's business compliance. Testing could vary from system to system and can be considerably different as per the architectural style of the application. Everything depends on how we are strategizing our testing approach or plan; for example, testing a monolith .NET application will be different as compared to testing SOA or microservices. In this chapter, we will cover these topics:

  • How to test microservices
  • Handling challenges
  • Testing strategies
  • The testing pyramid
  • Types of microservice tests

How to test microservices


Testing microservices could be a challenging job as it is different from how we test applications built using the traditional architectural style. In a .NET monolithic application, testing is a bit easier compared to microservices, which provides implementation independence and short delivery cycles.

Let's understand it in the context of our .NET monolithic application, where we did not utilize continuous integration and deployment. It becomes more complex when testing is combined with continuous integration and deployment. In microservices, we will be required to understand the tests for every service and how these tests differ from each other. Also, note that automated testing does not mean that we will not perform any manual testing at all.

Here are a few things that make microservice testing a complex and challenging task:

  • Microservices might have multiple services that work together or individually for an enterprise system, so they can be complex.
  • Microservices...

Testing strategies (testing approach)


As mentioned in the prerequisites section of Chapter 1, What are Microservices?, deployment and QA requirements would become more demanding. The only way to effectively handle this scenario would be through preemptive planning. I have always favored the representation of the QA team during the early requirement gathering and design phase. In the case of microservices, it becomes a necessity to have a close collaboration between the architecture group and the QA group. Not only would the QA group input be helpful, but the QA group would be able to draw up a strategy to test the microservices effectively.

Test strategies are nothing but a map or outlined plan that describes the complete approach of testing.

Different systems require different testing approaches. It is not possible to implement a pure testing approach to a system that is developed using a newer approach rather than the earlier developed system. Testing strategies should be clear to everyone...

Testing pyramid


The testing pyramid is a strategy or a way to define what you should test in microservices. In other words, we can say it helps us define the testing scope of microservices. The concept of the testing pyramid was originated by Mike Cohn (http://www.mountaingoatsoftware.com/blog/the-forgotten-layer-of-the-test-automation-pyramid) in 2009. There are various flavors of the testing pyramid; different authors have described this by indicating how they had placed or prioritized their testing scope. The following image depicts the same concept that was defined by Mike Cohn:

The Testing pyramid showcases how a well-designed test strategy is structured. When we closely look at it, we can easily notice how we should follow the testing approach for microservices (note that the testing pyramid is not specific to microservices). Let's start from the bottom of this pyramid. We can see that the testing scope is very limited with the use of Unit tests. As soon as we move to the top, our testing...

Types of microservice tests


In the previous section, we discussed test approaches or testing strategies. These strategies decide how we will proceed with the testing of the system. In this section, we will discuss various types of microservice testing.

Unit testing

Unit tests are tests that typically test a single function call to ensure that the smallest piece of the program is tested. So these tests are meant to verify specific functionality without considering other components:

  • Testing would be more complex when components are broken down into small, independent pieces and that are supposed to be tested independently. Here, testing strategies come in handy and ensure that the best quality assurance of a system would be performed. It adds more power when it comes along with the Test-Driven Development (TDD) approach.
  • Unit tests are of any size, or say, there is no definition for the size of unit tests. Generally, these tests are written at the class level.
  • Smaller unit tests are good to test...

Tests in action


Until now, we have discussed test strategies and various types of microservice tests. We've also discussed how to test and what to test. In this section, we will see tests in action; we will implement tests with the use of:

  • Visual Studio 2017 RC or later
  • .NET Core
  • ASP.NET Core API
  • xUnit and MS tests
  • The Moq framework 

 Getting ready with the test project

We will test our microservice application: FlixOne bookstore. With the help of code examples, we will see how to perform unit tests, stubbing, and mocking.

Note

We created the FlixOne bookstore application in Chapter 2, Building Microservices.

Before we start writing tests, we should set up a test project in our existing application. There are a few simple steps we can take with this test project setup:

  • Using Visual Studio, add a new .NET Core (class library) project to your existing solution
  • You can alternatively use the cli command to add the new project--open the bash command or the Visual Studio command prompt and execute dotnet...

Summary


Testing microservices is a bit different from applications built on the traditional architectural style. In a .NET monolithic application, testing is a bit easier as compared to microservices, and it provides implementation independence and short delivery cycles. Microservices face challenges while performing the testing. With the help of the testing pyramid concept, we can strategize how to go with testing. Referring to the testing pyramid, we can easily see that unit tests provide the facility to test a small function of a class and are less time-consuming. On the other hand, the top layer of the testing pyramid enters a large scope with system or end-to-end testing, and these tests are time taking and much expensive. Consumer-driven contracts are a very useful way to test microservices. Pact-net is an open source tool meant for this. Finally, we went through the actual test implementation.

In the next chapter, we will see how to deploy a microservice application. We will discuss...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Building Microservices with .NET Core
Published in: Jun 2017Publisher: PacktISBN-13: 9781785887833
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Gaurav Aroraa

Gaurav Aroraa has done M.Phil in computer science. He is a Microsoft MVP, life time member of Computer Society of India (CSI), certified as a scrum trainer/coach, XEN for ITIL-F and APMG for PRINCE-F and PRINCE-P. Gaurav serves as a mentor at IndiaMentor, open source developer, contributor to TechNet Wiki co-founder of Innatus Curo Software LLC. In 19+ years of his career, he has mentored thousands of students and industry professionals. You can tweet Gaurav on his twitter handle @g_arora
Read more about Gaurav Aroraa

author image
Lalit Kale

Lalit Kale is a technical architect and consultant with more than 12 years of industry experience. Lalit has helped clients achieve tangible business outcomes through the implementation of best practices in software development. He is a practitioner of TDD and DDD, and a big believer in agile and lean methodologies. He has worked with several organizations, from start-ups to large enterprises, in making their systems successful, be it in-house or mission critical, with clients in the USA, the UK, Germany, Ireland, and India. His current interests include container technologies and machine learning using Python. He holds a bachelor's degree in engineering (IT).
Read more about Lalit Kale

author image
Manish Kanwar

Manish Kanwar completed his masters of science in computer applications from MD University, India, and is a cofounder of Innatus Curo Software LLC, with a presence in India. He has been working in the IT industry across domains for the last 17 years. He started exploring .NET right from the first release and has been glued to it ever since. His range of experience includes global wealth management (financial service industry, USA), life insurance (insurance industry, USA), and document management system (DMS), ECMS, India. Manish does his bit for the community by helping young professionals through the IndiaMentor platform.
Read more about Manish Kanwar