Tag Archives: Unit Testing
Unit Tests & Data Coverage for Machine Learning Models
This post represents thoughts on what would it look like planning unit tests for machine learning models. The idea is to perform automated testing of ML models as part of regular builds to check for regression related errors in terms of whether the predictions made by certain set of input data vectors does not match with expected outcomes. This brings up some of the following topics for discussion: Why unit testing for machine learning models? What would unit tests for machine learning models mean? Data coverage or code coverage? Why unit testing for Machine Learning models? Once a model is built, the challenge is to monitor the performance metrics of the models …
Unit Testing Interview Questions – Set 1
This page lists down the practice tests / interview questions and answers for unit testing (general concepts) which applies for all programming languages. Those wanting to test their unit testing knowledge would find this test useful enough. The goal for this practice test is to help you do quick check of your knowledge in unit testing and prepare appropriately. More importantly, when you are preparing for interviews, these practice tests are intended to be handy enough. Those going for freshers / intern interviews in the area of programming language or for developer positions would also find these practice tests / interview questions to be very helpful. Following topics have been covered as part of …
Javascript – Jasmine Unit Tests for Promise Object
This article represents tips and code samples in relation with how to write unit tests for Promise object when using Jasmine framework. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Following are the key points described later in this article: Sample Promise Object Unit Tests related with Promise Object Sample Promise Object Pay attention to some of the following in the code below: Auth module returns a Promise object that returns a User object when resolved or returns an error object in case of error. A JSON object is passed to resolve or reject method. Save the file …
AngularJS Hello World Unit Testing Code Example
The article intends to provide take away code to get started with unit testing while working with AngularJS. The underlying Javascript unit testing framework used for testing the AngularJS code sample is Jasmine. Controllers.js ControllersSpecs.js You may want to pay attention to some of the following facts in relation with unit testing vis-a-vis unit testing controller methods: Use AngularJS dependency injection feature to pass on dependencies to Angular components such as controller, filters, directives etc. Following code does the magic of injecting $rootScope and $controller which is also represented in the code samples later in this article. For rookies, following should be sufficient for cut-copy-paste to get started with unit …
Quick Reference to Jasmine – Javascript Unit Testing Framework
The article represents the fundamentals concepts and code samples around Jasmine, a popular Javascript unit testing framework.TO get started, download the framework from https://github.com/pivotal/jasmine/releases. For detailed examples, look at http://jasmine.github.io/edge/introduction.html. It All Starts with “describe” and “it” Function Blocks In Jasmine terminologies, a set of tests is collectively called as a “suite”. The test suite is defined using “describe” function block. Each test is called as a “spec” and defined using “it” function block. A test suite can have multiple test specs and also, test suites. This implies that a “describe” function block can have multiple “it” function blocks and also, “describe” function blocks contained within. One of the key …
Unit Testing with JUnit & Mockito – Code Examples
The article presents quick code example for doing unit testing with JUnit & Mockito mocking framework. It also represents unit testing naming convention where features text is used for naming the test methods thereby representing test class as alternate documentation for the class under test. Following is done while defining Mock member variables Use @Mock annotation ahead of member variable definitions Following are steps done in the setup method: Initialize the mock using MockitoAnnotations.initMocks(this); Object of class under test is initialized with the mock objects Following steps are used in each of the unit test methods: Create/Initialize required objects for working with mocks and methods under test Set the mock …
7 Popular Unit Test Naming Conventions
The article presents a compiled list of unit tests naming strategy that one could follow for naming their unit tests. The article is intended to be a quick reference instead of going through multiple great pages such as following. That said, to know greater details, please feel free access one of these pages listed below and know for yourself. Naming Standards for Unit Tests Naming Unit Tests Responsibly What are some popular naming conventions for unit tests? Unit Tests Naming Best Practices GivenWhenThen Technique Following are 7 popular unit tests naming conventions that are found to be used by majority of developers and compiled from above pages: MethodName_StateUnderTest_ExpectedBehavior: There are …
AngularJS – 7 Steps for Unit Testing AngularJS Scripts with Jasmine
This article presents instructions on what would it take to create a setup and run unit testing for your AngularJS scripts using Jasmine framework. I shall be presenting various samples on my AngularJS website, http://hello-angularjs.appspot.com. Sorry for the typos in this article and please feel free to suggest/add any points if I missed them out. Following are key steps: Learn Jasmine Create Unit Tests using Jasmine Download and install NodeJS Download & Install Karma Configure Karma Place Scripts Appropriately Execute Tests Learn Jasmine Jasmine, one of the very popular Javascript unit testing framework, can be used for unit testing one’s AngularJS scripts. As a matter of fact, Angular …
Javascript Unit Testing using Jasmine – Code Examples
The article lists down some of the unit tests samples for testing Javascript code. The unit tests in this article tests the javascript code presented in this article, “What are Objects in Javascript?”. Before presenting code samples, lets try and understand what is Jasmine? As on the Jasmine website, it is defined as a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests. By what I learnt so far, I could vouch for the statement “And it has a clean, obvious syntax so that …
Java Unit Testing Interview Questions
The article presents some of the frequently asked interview questions in relation with unit testing with Java code. Please suggest other questions tthat you came across and I shall include in the list below. What is unit testing? Which unit testing framework did you use? What are some of the common Java unit testing frameworks? Ans: Read the definition of Unit testing on Wikipedia page for unit testing. Simply speaking, unit testing is about testing a block of code in isolation. There are two popular unit testing framework in Java named as Junit, TestNG. In SDLC, When is the right time to start writing unit tests? Ans: Test-along if …
Test-Driven Development (TDD) Shines with Mocking
The article presents a perspective and some code samples on how one could some cool stuff with Test-driven development (TDD) and Mocking. The code samples are done in Java. Lets briefly understand what is TDD and mocking? What is TDD? Test-driven development, simply speaking, is a software development process in which developers write tests first and, then writing enough code to pass those tests. Once all of the tests pass, they do code refactoring to enhance code quality. Following are key advantages of adopting TDD as your development process: Enforces the developers to thoroughly think through various different test cases which could be used to test the functional/business requirements. It, …
Unit Tests – Mocking vs Stubbing Vs Spying
The article lists down some good reads (from different webpages on internet) on unit testing concepts such as mocking, stubbing and spying and represents summarized information on these concepts. Mocks Aren’t Stubs What’s the difference between a mock & stub? Mocking vs. Spying in mocking frameworks Mocking and stubbing in Ruby on Rails What’s the difference between faking, mocking, and stubbing? After going through all the above pages, I could arrive at following definitions: Mocking Mocking is a unit testing phenomenon which is used to replace real objects with test objects that mocks the behavior. These test objects are called as Mock objects. The point to note is that mock …
Top 5 Unit Testing Best Practices
The article lists down top 5 unit testing best practices that one could adopt to be able to write great unit tests with greater code coverage. Test-Along, if not Test-Driven Many a times, rookies have been found to start writing unit tests once they have written most of the code, e.g., towards the end of the coding. However, to make the most of unit tests, one should write them as they write the code. This is somewhat different and less difficult than writing tests first and code later as in test-driven development (TDD). Following are some of the advantages of doing “test-along” development: One could achieve greater code coverage by …
Junit & Mock Framework Mockito Code Samples – Part 1
The article represents code samples for Junit tests and Mockito, a mocking framework. In addition, it describes different aspects of unit testing and mocking. Software Requirement The goal is to create a simple piece of software which caters to the requirement of school admission where in applicants submit their admission application. Class Design & JUnit Tests To meet above requirement, following different component is designed: Core Components (Key Ones) AdmissionApplication.java: Consists of method recordNewApplication which validates the submitted application, and later, stores in the database after successful validation. ApplicationValidation.java: Consists of methods for validating business rules associated with admission such as mandatory inputs (first name, last name, class, gender, …
JUnit Tests Code Samples for Testing Exception Scenarios
The article presents an example of unit tests which tests both happy path and exception scenario. Business Requirement User trying to open an account must be validated against the business rules related with users’ registration. Following are different business rules: User must provide a valid email address User must provide his first name User’s age must be 18 and above Class Design To demonstrate the unit tests with happy and exception scenarios, following classes have been shown below: SignupValidation: Consists of code validating business rules SignupException: Custom exception representing Signup business rules failure EmailValidator (Get the code from this page). SignupValidationTests: Unit tests for testing signup validation code Class …
Test-Along Development (TAD): Unit testing technique
While talking to one of the client stakeholder, I came across this interesting term as Test-Along Development. Following are two different techniques I came across as common way of writing unit test: Test-driven development where we write unit tests first and then write enough code to make unit tests pass Write unit tests at the end once you are done with the development. Both of the above techniques presented enough challenges to the software developers. Some of them are following: Test-driven development: In agile development, in 2 week sprints, the design took much longer than 2-3 days. Thus, doing test-driven development was found to lead carry-overs for sprint given on …
I found it very helpful. However the differences are not too understandable for me