The document discusses unit testing and test-driven development. It covers different types of tests like state verification and behavior verification tests. It provides examples of how to write unit tests for a sample InvitationService class. It also discusses test design patterns like test data builders and test data providers that allow writing DRY and readable unit tests. Finally, it covers best practices for writing clean unit tests and some common pitfalls to avoid when testing code.
This document provides an introduction to using the Google Test framework for unit testing C++ code. It begins with an example of a simple test for a function called calc_isect. It then demonstrates how to add assertions to tests, use test fixtures to reduce duplicated setup code, and generate parameterized tests. The document also covers best practices for test organization, installing and using Google Test, and some key features like XML output and selecting subsets of tests. Overall, the document serves as a tutorial for getting started with the Google Test framework for writing and running unit tests in C++ projects.
This document provides an introduction to unit testing, including what it is, why it's important, and how to implement it. It defines a unit as a class or function, and says unit tests should test these individual components in isolation. Key reasons for unit testing include enabling refactoring, maintaining code, and testing functionality before bugs or features are introduced. The document outlines guidelines for writing unit tests, such as having one test per scenario/assertion, and explains how to isolate dependencies using techniques like dependency injection, mocks, and stubs. It also discusses tools for automating testing and measuring code coverage.
This document provides an overview of object-oriented programming concepts in Java, including classes, objects, variables, methods, constructors, abstraction, encapsulation, inheritance, and polymorphism. It defines classes and objects, and describes how classes act as blueprints for objects. It explains the syntax for defining classes and class members like variables and methods. It also covers method overloading, different types of methods, and how constructors are used to initialize objects. The document concludes with brief explanations of abstraction, encapsulation, inheritance, and polymorphism as fundamental principles of object-oriented design.
Использование GMock для обеспечения спокойной и сытой жизни разработчика. Обзор как верхушки так и некоторых подводных частей GMock. Разбор возможностей фреймворка на примерах.
Tech In Asia PDC 2017 - Best practice unit testing in mobile appsFandy Gotama
This document provides best practices for writing unit tests for mobile apps. It recommends refactoring code before writing unit tests, using clean architecture approaches, and convincing teammates of the importance of testing. It also suggests testing the smallest parts of code individually, avoiding logic in tests, and making tests trustworthy, maintainable, and readable. The document demonstrates examples of good unit tests and emphasizes loosely coupling code and dependency injection to facilitate testing. It provides resources for further reading on unit testing.
"Unit Testing for Mobile App" by Fandy Gotama (OLX Indonesia)Tech in Asia ID
Currently, Fandy is the mobile apps tech lead works for OLX Indonesia and has been working on both Android and iOS since Cupcake and iOS 2.x respectively. As a tech lead he is responsible for delivering software into production safely and quickly in a sustainable way.
***
This slide was shared at Tech in Asia Product Development Conference 2017 (PDC'17) on 9-10 August 2017.
Get more insightful updates from TIA by subscribing techin.asia/updateselalu
Unit testing allows testing individual units of code in isolation using Spock, a testing framework for Java and Groovy. Spock specifications extend Specification and contain fixture methods like setup() and feature methods to define test cases and expected behavior. Feature methods use blocks like when, then, and expect to define stimuli and verify outputs. Spock supports data-driven testing using a data table and mocking dependencies using Mock() to focus testing on the unit. Basic Spock commands include running tests with grails test-app and viewing test reports.
This document provides an introduction to using the Google Test framework for unit testing C++ code. It begins with an example of a simple test for a function called calc_isect. It then demonstrates how to add assertions to tests, use test fixtures to reduce duplicated setup code, and generate parameterized tests. The document also covers best practices for test organization, installing and using Google Test, and some key features like XML output and selecting subsets of tests. Overall, the document serves as a tutorial for getting started with the Google Test framework for writing and running unit tests in C++ projects.
This document provides an introduction to unit testing, including what it is, why it's important, and how to implement it. It defines a unit as a class or function, and says unit tests should test these individual components in isolation. Key reasons for unit testing include enabling refactoring, maintaining code, and testing functionality before bugs or features are introduced. The document outlines guidelines for writing unit tests, such as having one test per scenario/assertion, and explains how to isolate dependencies using techniques like dependency injection, mocks, and stubs. It also discusses tools for automating testing and measuring code coverage.
This document provides an overview of object-oriented programming concepts in Java, including classes, objects, variables, methods, constructors, abstraction, encapsulation, inheritance, and polymorphism. It defines classes and objects, and describes how classes act as blueprints for objects. It explains the syntax for defining classes and class members like variables and methods. It also covers method overloading, different types of methods, and how constructors are used to initialize objects. The document concludes with brief explanations of abstraction, encapsulation, inheritance, and polymorphism as fundamental principles of object-oriented design.
Использование GMock для обеспечения спокойной и сытой жизни разработчика. Обзор как верхушки так и некоторых подводных частей GMock. Разбор возможностей фреймворка на примерах.
Tech In Asia PDC 2017 - Best practice unit testing in mobile appsFandy Gotama
This document provides best practices for writing unit tests for mobile apps. It recommends refactoring code before writing unit tests, using clean architecture approaches, and convincing teammates of the importance of testing. It also suggests testing the smallest parts of code individually, avoiding logic in tests, and making tests trustworthy, maintainable, and readable. The document demonstrates examples of good unit tests and emphasizes loosely coupling code and dependency injection to facilitate testing. It provides resources for further reading on unit testing.
"Unit Testing for Mobile App" by Fandy Gotama (OLX Indonesia)Tech in Asia ID
Currently, Fandy is the mobile apps tech lead works for OLX Indonesia and has been working on both Android and iOS since Cupcake and iOS 2.x respectively. As a tech lead he is responsible for delivering software into production safely and quickly in a sustainable way.
***
This slide was shared at Tech in Asia Product Development Conference 2017 (PDC'17) on 9-10 August 2017.
Get more insightful updates from TIA by subscribing techin.asia/updateselalu
Unit testing allows testing individual units of code in isolation using Spock, a testing framework for Java and Groovy. Spock specifications extend Specification and contain fixture methods like setup() and feature methods to define test cases and expected behavior. Feature methods use blocks like when, then, and expect to define stimuli and verify outputs. Spock supports data-driven testing using a data table and mocking dependencies using Mock() to focus testing on the unit. Basic Spock commands include running tests with grails test-app and viewing test reports.
This document provides an overview of testing in Grails and Spock testing framework. It discusses unit testing, the advantages of unit testing like facilitating change and improving quality. It also covers Spock basics like specification, fields, blocks, expectations. It demonstrates how to write unit tests in Spock including mocking, stubbing, data-driven testing and verification of interactions. The document recommends using the code coverage plugin to analyze test coverage.
The document discusses unit testing in Grails using the Spock testing framework. It covers the basics of unit testing including goals, advantages, and challenges. It then provides an overview of Spock and examples of writing unit tests in Spock including mocking methods, domains, configurations, and dependencies. The document also discusses how to write unit tests for controllers and integration tests in Grails using Spock.
Unit testing patterns for concurrent codeDror Helper
Getting started with unit testing is not hard, the only problem is that most programs are more than a simple calculator with two parameters and a returns value that is easy to verify.
Writing unit tests for multi-threaded code is harder still.
Over the years I discovered useful patterns that helped me to test multi-threaded and asynchronous code and enabled the creation of deterministic, simple and robust unit tests.
Come learn how to test code that uses concurrency and parallelism – so that the excuses of not writing unit tests for such code would become as obsolete as a single core processors.
Thermostat example demonstrates dependency injection principles. Initially, the naive Thermostat class directly creates its dependencies like the TemperatureSensor class. This leads to tight coupling. To decouple the classes, an interface TemperatureSensor is extracted. The dependency is then injected via the constructor rather than created internally. This allows injecting different sensor implementations and makes the Thermostat class more reusable and testable.
The document outlines a book on JUnit recipes for testing Java code. It is divided into three parts that cover the basics of JUnit, testing Java 2 Enterprise Edition (J2EE) applications, and advanced JUnit techniques. The first part introduces fundamentals like unit testing objects in isolation, the structure of writing test cases using the JUnit framework, and assertions. It also describes test-driven development and lists common elementary tests like testing equals methods, getters, setters, exceptions, and complex objects.
Дмитрий Контрерас «Back to the future: the evolution of the Java Type System»Anna Shymchenko
This document discusses the evolution of the Java type system, including changes in Java 5-8 and future projects. Java 5 introduced generics and wildcards. Java 8 added generalized type inference and type annotations. The Checker Framework provides type checkers for properties like nullness and tainting. Project Valhalla aims to add value types and generic specialization to Java.
This document discusses best practices for writing unit tests. It emphasizes that unit tests should be easy to understand, trustworthy, and robust. A good unit test only fails if a bug is introduced or requirements change. The document provides examples of well-written unit tests and discusses how to avoid common problems like fragile tests. It stresses the importance of testing only specified functionality, using isolation frameworks to mock dependencies, and not putting test logic in the test. Overall, the document promotes writing "clean tests" to avoid brittle tests and minimize maintenance overhead.
Test-driven development (TDD) is a software development process that relies on the repetition of short development cycles called the TDD cycle. The TDD cycle involves first writing a test case that defines a desired improvement or new function, then producing code to pass that test and finally refactoring the new code to acceptable standards. The document discusses TDD training which includes topics like fixtures, assertion patterns, test quality and a case study. It motivates TDD by explaining how it helps build quality code, improves maintainability and meets client needs by focusing on internal and external quality. Key aspects of TDD like the AAA test format and strategies for selecting the next test are also covered. Finally, the document reviews evidence from case
How difficult is to automatically test the HelloWorld.
We fix it and other many difficult scenarios with techniques like:
- lower "s" singleton
- law of demeter
- dependency injection
- and more examples
TDD? Sure, but What About My Legacy Code?Rob Myers
This document discusses techniques for adding tests to legacy code, which is defined as code that is difficult to change without tests. It recommends choosing what code to test based on what needs to change now or changes frequently. It describes techniques like writing "pinning" tests that don't change behavior, using the "Three Questions" to identify obstacles to testing, separating behavior from initialization, exposing private methods, wrapping untestable code with proxies, and using factory method injection to inject mocks. The goal is to incrementally add tests to legacy code to enable refactoring it safely.
This document summarizes lessons learned from building MongoDB and MongoEngine. Some key lessons include: dive in and start contributing to open source projects to help them progress; metclasses are an important tool that allow ORM functionality to be added to classes; not all new ideas are good and it's important to avoid straying too far from existing patterns that users expect; tracking changes at a granular level allows partial updates but adds complexity. Overall it encourages contributors to learn why certain approaches were taken and focus on improving existing designs rather than introducing radical changes.
JUnit is an open source unit testing framework for Java applications. It was written by Erich Gamma and Kent Beck and is part of the xUnit family of unit testing frameworks. JUnit allows developers to write test cases that test individual classes and methods. These test cases can be grouped into test suites and run automatically to ensure code meets requirements and to catch errors early in the development process. JUnit includes annotations like @Test to identify test methods and asserts to validate method output. It integrates with development tools like Eclipse to provide test results.
The document discusses various object-oriented programming concepts in Java including inheritance, subclasses, overriding methods, constructors, and modifiers for classes, variables, constructors, and methods. It provides examples to demonstrate inheritance hierarchies, accessing variables and methods from superclasses, constructor invocation order, abstract classes, and usage of access modifiers like public, private, and protected.
Introduction to web programming for java and c# programmers by @drpicoxDavid Rodenas
(better presented by @drpicox)
Slides of an introductory course for web programming focusing in basic Javascript and CSS concepts. It assumes knowledge of programming, Java or C#.
Generating characterization tests for legacy codeJonas Follesø
This document discusses generating characterization tests for legacy code. It describes characterization tests as tests that characterize the actual behavior of code to act as change detectors and protect legacy code from unintended changes. It provides an example of a characterization test for a calculation method, and discusses how to find "pinch points" in code to focus testing and how to structure tests to replay inputs and outputs from recorded method executions. The document also introduces a framework for writing characterization tests by recording method inputs, outputs, and dependencies.
Automatic assessment of code to support education is an important feature of many programming learning platforms. Unit testing frameworks can be used to perform a systematic functional test of codes; they are mainly used by developers. Competition graders can be used to safely execute code in sandboxed environments; they are mainly used for programming contests. This paper proposes a platform combining the advantages of unit testing and competition graders to provide a unit testing-based grader. The proposed platform assesses codes and produces relevant and "intelligent" feedbacks to support learning. The paper presents the architecture of the platform and how the unit tests are designed.
This document discusses unit testing using PowerMock and Mockito frameworks. It explains reasons for and against unit testing and covers testing public API, static, and private methods. PowerMockito is used for mocking static methods and invoking private methods directly. The document also provides tips for writing good unit tests and references for further reading.
This document provides an overview of how to create test classes and methods in JUnit and mock objects using frameworks like PowerMock and EasyMock. It describes how to define a test class that extends TestCase and overrides setUp(), tearDown(), and testXXX() methods. It also explains how PowerMock allows mocking static methods, private methods, final classes/methods by using annotations and bytecode manipulation.
This document defines and describes various types of test doubles (dummy objects, fakes, stubs, mocks, spies) that are used for testing purposes. It provides examples of how to create mocks using Mockito, stub behaviors for mocks, verify interactions with mocks, and some limitations of mocking with Mockito.
Breaking Dependencies to Allow Unit TestingSteven Smith
Unit testing software can be difficult, especially when the software wasn't designed to be testable. Dependencies on infrastructure concerns and software we don't control are one of the biggest contributors to testing difficulty. In this session, you'll learn the difference between unit tests and other kinds of tests, how to recognize and invert dependencies, and how to unit test your code's interactions with these dependencies without testing the infrastructure itself.
Presented at FalafelCON 2014, San Francisco, September 2014
This document provides an overview of testing in Grails and Spock testing framework. It discusses unit testing, the advantages of unit testing like facilitating change and improving quality. It also covers Spock basics like specification, fields, blocks, expectations. It demonstrates how to write unit tests in Spock including mocking, stubbing, data-driven testing and verification of interactions. The document recommends using the code coverage plugin to analyze test coverage.
The document discusses unit testing in Grails using the Spock testing framework. It covers the basics of unit testing including goals, advantages, and challenges. It then provides an overview of Spock and examples of writing unit tests in Spock including mocking methods, domains, configurations, and dependencies. The document also discusses how to write unit tests for controllers and integration tests in Grails using Spock.
Unit testing patterns for concurrent codeDror Helper
Getting started with unit testing is not hard, the only problem is that most programs are more than a simple calculator with two parameters and a returns value that is easy to verify.
Writing unit tests for multi-threaded code is harder still.
Over the years I discovered useful patterns that helped me to test multi-threaded and asynchronous code and enabled the creation of deterministic, simple and robust unit tests.
Come learn how to test code that uses concurrency and parallelism – so that the excuses of not writing unit tests for such code would become as obsolete as a single core processors.
Thermostat example demonstrates dependency injection principles. Initially, the naive Thermostat class directly creates its dependencies like the TemperatureSensor class. This leads to tight coupling. To decouple the classes, an interface TemperatureSensor is extracted. The dependency is then injected via the constructor rather than created internally. This allows injecting different sensor implementations and makes the Thermostat class more reusable and testable.
The document outlines a book on JUnit recipes for testing Java code. It is divided into three parts that cover the basics of JUnit, testing Java 2 Enterprise Edition (J2EE) applications, and advanced JUnit techniques. The first part introduces fundamentals like unit testing objects in isolation, the structure of writing test cases using the JUnit framework, and assertions. It also describes test-driven development and lists common elementary tests like testing equals methods, getters, setters, exceptions, and complex objects.
Дмитрий Контрерас «Back to the future: the evolution of the Java Type System»Anna Shymchenko
This document discusses the evolution of the Java type system, including changes in Java 5-8 and future projects. Java 5 introduced generics and wildcards. Java 8 added generalized type inference and type annotations. The Checker Framework provides type checkers for properties like nullness and tainting. Project Valhalla aims to add value types and generic specialization to Java.
This document discusses best practices for writing unit tests. It emphasizes that unit tests should be easy to understand, trustworthy, and robust. A good unit test only fails if a bug is introduced or requirements change. The document provides examples of well-written unit tests and discusses how to avoid common problems like fragile tests. It stresses the importance of testing only specified functionality, using isolation frameworks to mock dependencies, and not putting test logic in the test. Overall, the document promotes writing "clean tests" to avoid brittle tests and minimize maintenance overhead.
Test-driven development (TDD) is a software development process that relies on the repetition of short development cycles called the TDD cycle. The TDD cycle involves first writing a test case that defines a desired improvement or new function, then producing code to pass that test and finally refactoring the new code to acceptable standards. The document discusses TDD training which includes topics like fixtures, assertion patterns, test quality and a case study. It motivates TDD by explaining how it helps build quality code, improves maintainability and meets client needs by focusing on internal and external quality. Key aspects of TDD like the AAA test format and strategies for selecting the next test are also covered. Finally, the document reviews evidence from case
How difficult is to automatically test the HelloWorld.
We fix it and other many difficult scenarios with techniques like:
- lower "s" singleton
- law of demeter
- dependency injection
- and more examples
TDD? Sure, but What About My Legacy Code?Rob Myers
This document discusses techniques for adding tests to legacy code, which is defined as code that is difficult to change without tests. It recommends choosing what code to test based on what needs to change now or changes frequently. It describes techniques like writing "pinning" tests that don't change behavior, using the "Three Questions" to identify obstacles to testing, separating behavior from initialization, exposing private methods, wrapping untestable code with proxies, and using factory method injection to inject mocks. The goal is to incrementally add tests to legacy code to enable refactoring it safely.
This document summarizes lessons learned from building MongoDB and MongoEngine. Some key lessons include: dive in and start contributing to open source projects to help them progress; metclasses are an important tool that allow ORM functionality to be added to classes; not all new ideas are good and it's important to avoid straying too far from existing patterns that users expect; tracking changes at a granular level allows partial updates but adds complexity. Overall it encourages contributors to learn why certain approaches were taken and focus on improving existing designs rather than introducing radical changes.
JUnit is an open source unit testing framework for Java applications. It was written by Erich Gamma and Kent Beck and is part of the xUnit family of unit testing frameworks. JUnit allows developers to write test cases that test individual classes and methods. These test cases can be grouped into test suites and run automatically to ensure code meets requirements and to catch errors early in the development process. JUnit includes annotations like @Test to identify test methods and asserts to validate method output. It integrates with development tools like Eclipse to provide test results.
The document discusses various object-oriented programming concepts in Java including inheritance, subclasses, overriding methods, constructors, and modifiers for classes, variables, constructors, and methods. It provides examples to demonstrate inheritance hierarchies, accessing variables and methods from superclasses, constructor invocation order, abstract classes, and usage of access modifiers like public, private, and protected.
Introduction to web programming for java and c# programmers by @drpicoxDavid Rodenas
(better presented by @drpicox)
Slides of an introductory course for web programming focusing in basic Javascript and CSS concepts. It assumes knowledge of programming, Java or C#.
Generating characterization tests for legacy codeJonas Follesø
This document discusses generating characterization tests for legacy code. It describes characterization tests as tests that characterize the actual behavior of code to act as change detectors and protect legacy code from unintended changes. It provides an example of a characterization test for a calculation method, and discusses how to find "pinch points" in code to focus testing and how to structure tests to replay inputs and outputs from recorded method executions. The document also introduces a framework for writing characterization tests by recording method inputs, outputs, and dependencies.
Automatic assessment of code to support education is an important feature of many programming learning platforms. Unit testing frameworks can be used to perform a systematic functional test of codes; they are mainly used by developers. Competition graders can be used to safely execute code in sandboxed environments; they are mainly used for programming contests. This paper proposes a platform combining the advantages of unit testing and competition graders to provide a unit testing-based grader. The proposed platform assesses codes and produces relevant and "intelligent" feedbacks to support learning. The paper presents the architecture of the platform and how the unit tests are designed.
This document discusses unit testing using PowerMock and Mockito frameworks. It explains reasons for and against unit testing and covers testing public API, static, and private methods. PowerMockito is used for mocking static methods and invoking private methods directly. The document also provides tips for writing good unit tests and references for further reading.
This document provides an overview of how to create test classes and methods in JUnit and mock objects using frameworks like PowerMock and EasyMock. It describes how to define a test class that extends TestCase and overrides setUp(), tearDown(), and testXXX() methods. It also explains how PowerMock allows mocking static methods, private methods, final classes/methods by using annotations and bytecode manipulation.
This document defines and describes various types of test doubles (dummy objects, fakes, stubs, mocks, spies) that are used for testing purposes. It provides examples of how to create mocks using Mockito, stub behaviors for mocks, verify interactions with mocks, and some limitations of mocking with Mockito.
Breaking Dependencies to Allow Unit TestingSteven Smith
Unit testing software can be difficult, especially when the software wasn't designed to be testable. Dependencies on infrastructure concerns and software we don't control are one of the biggest contributors to testing difficulty. In this session, you'll learn the difference between unit tests and other kinds of tests, how to recognize and invert dependencies, and how to unit test your code's interactions with these dependencies without testing the infrastructure itself.
Presented at FalafelCON 2014, San Francisco, September 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014FalafelSoftware
Unit testing software can be difficult, especially when the software wasn't designed to be testable. Dependencies on infrastructure concerns and software we don't control are one of the biggest contributors to testing difficulty. In this session, you'll learn the difference between unit tests and other kinds of tests, how to recognize and invert dependencies, and how to unit test your code's interactions with these dependencies without testing the infrastructure itself.
This document summarizes key points about developer testing based on a presentation given by Vaidas Pilkauskas. It discusses 15 things to know about testing, including test phases (setup, exercise, verify, teardown), testing behavior not methods, using matchers for assertions, custom matchers, ignoring tests, exceptions, asynchronous code, controlling time in tests, collections, access modifiers, random values, number of assertions, test doubles, naming conventions, and comments. The overall message is that tests should have clear intentions, follow best practices like the single responsibility principle, and focus on testing public behavior rather than implementation details.
Developer Tests - Things to Know (Vilnius JUG)vilniusjug
The document discusses various topics related to writing tests for software. It covers test structure and organization, what to test, using matchers, writing custom matchers, ignoring tests, handling exceptions, testing with time, testing collections, random values in tests, number of assertions per test, and decoupling in end-to-end tests. The presentation aims to provide best practices and anti-patterns to consider when writing and organizing test code.
Breaking Dependencies to Allow Unit TestingSteven Smith
Unit testing software can be difficult, especially when the software wasn't designed to be testable. Dependencies on infrastructure concerns and software we don't control are one of the biggest contributors to testing difficulty. In this session, you'll learn the difference between unit tests and other kinds of tests, how to recognize and invert dependencies, and how to unit test your code's interactions with these dependencies without testing the infrastructure itself.
TDD is a design technique where tests are written before code to determine requirements. The Red-Green-Refactor process is followed: 1) Write a test that fails (red), 2) Write minimum code to pass the test (green), 3) Refactor code. TDD ensures quality, keeps code simple and testable, and allows for rapid change. Writing tests first helps design by clarifying requirements and preventing bugs.
VT.NET 20160411: An Intro to Test Driven Development (TDD)Rob Hale
This document provides an introduction to test-driven development (TDD). It discusses the benefits of unit testing over end-to-end testing, including error localization, execution time, tighter feedback loops, and test coverage. It defines what constitutes a unit test and what does not. Examples of difficult scenarios to unit test are given. The basic TDD cycle of writing a failing test, making the test pass, and refactoring is explained. An exercise demonstrates applying TDD principles. Mock objects and the differences between the Chicago and London schools of TDD are briefly covered. Finally, additional resources on TDD are listed.
The Art of Unit Testing - Towards a Testable DesignVictor Rentea
Slides of the Talk I gave at Devoxx Belgium 2019.
=== Abstract ===
Focusing on the creative work without being terrified of breaking the existing behavior can make software development very addictive! Good automated tests can buy you that!
However, if your tests are not maintainable they may end up slowing you down and causing you painful headaches, compilation errors and spurious failures. To avoid that, your unit tests should be significant; expressive; clean; DRY; non-overlapping; and blazing fast. Writing good tests becomes the toughest challenge for any developer, no matter how battle-hardened: you need to balance risk with test maintenance costs, while looking out for test design smells that call for [risky] refactoring to drive your design towards a set of key principles (included:).
Principles that will end up shaping the way you craft the Production code itself. Because in the end, a good, clean design is more important than coverage%.
But testing gives you the best feedback to get there.
Grab a black coffee and join this snippet from Victor’s Pro Unit Testing #training, to learn about testing priorities, buggy tests, the shared @Before, Mocks vs Stubs and how to reduce them by "purifying" your logic, testing Legacy Code and refactoring @Spy-es out.
All of that in an entertaining, dynamic and memorable session.
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentAll Things Open
The document provides an overview of test-driven development (TDD) principles and how to implement testing for front-end development. It discusses the benefits of TDD, such as avoiding bad designs and preventing regressions. It also addresses common challenges with TDD, like slowing initial velocity and ignored tests over time. The document then covers different levels of testing, including unit tests, integration tests, and functional/end-to-end tests using the testing pyramid as a framework. It provides examples of writing tests for pure and impure functions using the Jest testing framework. Finally, it discusses functional testing using Cucumber.js behavior-driven development.
Shift-Left Testing: QA in a DevOps World by David LaulusaQA or the Highway
Shift-left testing involves injecting quality earlier in the software development process through techniques like unit testing, test-driven development, and regression testing. The presentation discusses principles for effective testing including equivalence partitioning, boundary value analysis, and combinatorial testing. It emphasizes the importance of collaboration between testers and developers through practices like dependency injection and test automation.
Unit testing involves writing automated tests to check units of code against expected behaviors. A unit test invokes a unit of work and checks a single assumption. It should be fast, consistent, isolated, and have a single responsibility. Best practices include testing in three steps (setup, act, assert), using mocks for dependencies, and organizing test code structure. Legacy code can be challenging to test but a common approach is to start with the most complex code and break dependencies.
Performance Test Driven Development with Oracle Coherencearagozin
This presentation discusses test driven development with Oracle Coherence. It outlines the philosophy of PTDD and challenges of testing Coherence, including the need for a cluster and sensitivity to network issues. It discusses automating tests using tools like NanoCloud for managing nodes and executing tests remotely. Different types of tests are described like microbenchmarks, performance regression tests, and bottleneck analysis. Common pitfalls of performance testing like fixed users vs fixed request rates are also covered.
Unit Testing Basics discusses the definition, types, benefits, and best practices of unit testing. The key points are:
- Unit tests verify small elements of code like classes or methods and have characteristics like being fast, isolated, repeatable, and self-checking.
- Different types of tests include unit tests, component tests, system tests, and functional tests which test at different scopes and have varying execution times.
- Well-designed unit tests provide benefits like documentation, error detection, and code improvements. Tests should be written before code using techniques like test-driven development.
- Frameworks like EasyMock can be used to create test doubles and mock objects to isolate the system under test and verify
Unit testing involves testing individual units or components of an application to ensure they operate as expected. The UnitTest++ and HippoMocks frameworks allow writing and running unit tests in C++. UnitTest++ provides macros and functions to define tests and make assertions while HippoMocks allows mocking dependencies to isolate units during testing.
Test and Behaviour Driven Development (TDD/BDD)Lars Thorup
In this introduction to Test Driven Development (TDD) or Behaviour Driven Development (BDD) we give a high level description of what it is and why it is useful for developers. Then we go into some details on stubs and mocks, test data, UI testing, SQL testing, JavaScript testing, web services testing and how to start doing TDD/BDD on an existing code base.
This document discusses different types of software testing including unit testing and integration testing. Unit testing involves writing code to test individual units or modules in isolation with few dependencies. Integration testing evaluates how modules function together as a group in a black box manner. The benefits of testing include facilitating code changes, enhancing software quality by catching bugs, serving as documentation, and improving code quality. Test-driven development is introduced as an approach where tests are written before code to drive the design. Continuous integration is also discussed as a way to fully automate testing, build, and code quality checks.
Building unit tests correctly with visual studio 2013Dror Helper
Unit testing is now considered a mainstream practice, but that does not mean it is as common, pervasive or as well understood as it could or should be. Many programmers struggle with the quality of their tests and with the focus of their code. In this session we’ll learn how to write good unit testing code.
Into The Box 2018 | Assert control over your legacy applicationsOrtus Solutions, Corp
This document discusses how to add tests to legacy code using the TestBox testing framework for ColdFusion applications. It defines unit testing and different types of tests like unit, integration, and end-to-end tests. TestBox allows writing automated tests for ColdFusion code and isolating dependencies using test doubles. The document outlines best practices for test-driven development including writing tests before code, using life cycle methods, and refactoring code without changing external behavior. Implementing a testing framework like TestBox helps assert control over legacy applications by adding tests and the ability to refactor code safely.
Core Java Programming Language (JSE) : Chapter VI - Class DesignWebStackAcademy
Class design that follows established principles is usually correct, reusable, extensible, and easy to maintain. But, that does not mean they are providence. A situation may arise where normal principles find it inadequate to fit in. In such a case, the purpose or objective should be the basic ingredient of design decision. But, such a situation may arise once in a blue moon. It is always a good idea to begin with a proper design guidelines rather than grope in the dark and become sorry in the later phases of development.
Before designing a class, we must understand that a class is an abstraction of a certain aspect of a problem. The objective is to filter those aspects that are important for the purpose and ignore those that are unimportant. Sometimes, given a problem, one may find it difficult to identify aspects that are the right candidate to be classified. After all, abstraction in computing is a poor representation of reality; even establishing a simple relationship can be quite complex. Still, they do represent because we insist. Depending upon the complexity of the problem, it can even be a challenge for a seasoned programmer. Perhaps, a way out of the situation is to focus on the purpose, because it is the purpose that determines what is to be taken into account and what to suppress. Also, depending upon the purpose to be served, a variety of abstractions of the same thing is possible. For example, a Media class construct may be designed differently depending upon the type of media it targets, such as print media or screen.
2. Why?
▪ Unit-test is the first client of your
code
▪ Verification and regression through
automation
▪ Powerful design tool
▪ Higher quality code
▪ Any many more…
2
3. Test Doubles
▪ Dummies
▪ Fakes
▪ Mock
▪ Stubs
Different types of mocks
▪ Pure mock vs nice mock
▪ Partial mocks and spies
3
4. Different type of tests
State Verification
▪ Execute one or many methods
of objects then assert expected
results.
▪ They keep continuing to pass
even if the internal of the method
changes.
Behavior Verification
▪ How the system behaves as it
operates rather than specifying
the expected end state.
▪ It verifies interaction between
the objects.
4
5. Example
public class InvitationService {
private final EmailService emailService;
private final EmailFilterService redList;
public InvitationService(EmailService
emailService, EmailFilterService redList) {
this.emailService = emailService;
this.redList = redList;
}
public void sendInvitation(Invitation invitation)
throws InviteeIsRedListed {
if (redList.redlisted(invitation.emailAddress)) {
throw new InviteeIsRedListed();
}
emailService.sendEmailInvite(invitation);
invitation.setIsSent(true);
}
}
5
6. State Verification Sample
@Test
public void testSendInvitation() throws InviteesRedListed {
InvitationService sut = new InvitationService(emailService,
emailFilterService);
Invitation invitation = new Invitation();
invitation.emailAddress = "1@mail.com";
sut.sendInvitation(invitation);
Assert.assertTrue(invitation.isSent());
}
@Test(expected = InviteesRedListed.class)
public void testSendInvitationGettingFiltered()
throws InviteesRedListed {
InvitationService sut = new InvitationService(emailService,
emailFilterService);
Invitation invitation = new Invitation();
invitation.emailAddress = "1@fake.com";
sut.sendInvitation(invitation);
} 6
7. Behavior Verification Sample
@Test
public void testSendInvitation() throws InviteesRedListed {
EmailService emailServiceMock = mock(EmailService.class);
EmailFilterService filterServiceMock = mock(EmailFilterService.class);
InvitationService sut = new InvitationService(emailServiceMock,
filterServiceMock);
Invitation invitation = new Invitation();
invitation.emailAddress = "1@test.com";
sut.sendInvitation(invitation);
// this is not important, if it is set by mock method then we can skip it.
Assert.assertTrue(invitation.isSent());
InOrder inOrder = inOrder(emailServiceMock, filterServiceMock);
inOrder.verify(filterServiceMock, times(1)).redlisted("1@test.com");
inOrder.verify(emailServiceMock, times(1)).sendEmailInvite(invitation);
}
7
8. Common fallacies of writing testable code
▪ Object construction in application logic
▪ Looking for things instead of asking for
them
▪ Doing too much work in the constructor
▪ Keeping global state
▪ Singleton pattern
▪ Static methods and utility classes
8
9. Example
public class ProfileService {
ProfileDataLayer dataLayer = new ProfileDataLayer();
ProfileValidator validator;
public ProfileService () {
validator = ProfileValidator.getValidator();
}
public CreateStatus create(String name, String imageUrl) {
try {
Profile profile = ProfileTransformer.transform(name, imageUrl);
validator.validate(profile);
dataLayer.insert(profile);
return CreateStatus.SUCCEED;
} catch (Exception e) {
return CreateStatus.FAILED;
}
}
Object construction
Looking for object
Static method call
9
10. Fixed example
public class ProfileService {
final ProfileDataLayer dataLayer;
final ProfileValidator validator;
final ProfileTransformer transformer
@Inject
public ProfileService (
ProfileDataLayer dataLayer,
ProfileValidator validator,
ProfileTransformer transformer) {
this.validator = validator;
this.dataLayer = dataLayer;
this.transformer = transformer;
}
Objects passed in
the constructor
through dependency
injection framework
10
11. Fixed example
public CreateStatus create(String name, String imageUrl) {
try {
Profile profile = transformer.transform(name, imageUrl);
validator.validate(profile);
dataLayer.insert(profile);
return CreateStatus.SUCCEED;
} catch (Exception e) {
return CreateStatus.FAILED;
}
}
No knowledge and
dependency on
implementation and
where logic comes
from.
11
12. Common fallacies of writing testable code
▪ Overusing inheritance as a method of code
reuse
▪ Lots of conditionals (IF and SWITCH
statements)
▪ Mixing value objects and service objects
▪ Breaking SRP (Single Responsibility
Principle)
12
13. Solutions overview
▪ Use dependency injection frameworks
▪ Favor composition over inheritance
▪ Favor polymorphism over conditions
▪ Command pattern, strategy patter, chain of
responsibility, etc
▪ Respect SRP
▪ RedFlag#1: Classes with bad postfix (Util,
Helper, Manager)
▪ RedFlag#2: Classes with more than 250 lines of
codes.
▪ RedFlag#3: Classes that have “And” or “OR” in
their names.
▪ RedFlag#4: Low Cohesion
▪ Validators, Transformation, Formatting, etc
should be in its own class.
13
14. Ultimate solution
▪ Test Driven Development - TDD
▪ Always prefer TDD for the system that you
own and you understanding the domain of
the problem very well.
▪ Test-after is OK when you are maintaining a
system or working on a horizontal feature
that you will need to change lots of other
people code.
14
15. Testing Legacy Code
▪ In this context Legacy code is a
code that is written and designed
without having testing in mind,
and it is still live code.
▪ They are usually hard to test.
15
16. Testing Legacy Code
▪ Refactoring is a powerful tool
▪ Introduce wrappers and
adapters
▪ Change access modifiers on
methods to make them visible
for testing
▪ public @interface VisibleForTesting {}
16
18. Coding Style
Production
▪ DRY (Don’t Repeat Yourself)
▪ High cohesion.
▪ Methods are interacting with
each other
▪ Lifecycle at class level
Unit Test
▪ DAMP (Descriptive And
Meaningful Phrases)
▪ Low cohesion
▪ Each test (method) are self-
contained
▪ Lifecycle at test methods.
▪ Meaningful and long method
names.
18
19. DAMP vs DRY
DRY example:
public void test1() {
setupTestFixture(ps1, ps2, ps3);
setupMocks(pm1, pm2);
verify(pv1, pv2, pv3);
}
▪ Not readable in its entirety.
19
20. Clean tests
▪ Avoid testing several things in one test
▪ When you need to persist data for your test,
persist in-memory
▪ Setup and cleanup your test
▪ Test must run fast. Really fast!
▪ No sleeping thread
20
21. Clean tests (part 2)
▪ Make your test have a Cyclomatic
Complexity of one. Tests should not have
indention or indirections.
▪ Beware of Liar anti-pattern, always observe
test failures
▪ Do not use try-catch blocks in your tests
▪ Do not be a mock-happy tester
▪ Do not target high code coverage
21
22. Test life cycle, threading and
frameworks
▪ Tests’ life cycle is separate from
applications
▪ For example TestNG runs Suite,
Group, Class, Test, Method
configurations before and after tests
▪ Tests have different threading models
▪ Parallel forks vs single threaded
▪ Frameworks do a lot of legwork
▪ Exception and failure handling
▪ Reports
▪ Tooling: Assertions, data providers,
factories, dependencies
22
23. Be mindful of scopes
@Mock Map<String, Integer> counters;
@BeforeTest
public void setUp() { MockitoAnnotations.initMocks(this); }
@Test public void testCount1() {
Mockito.when(counters.get(TEST_KEY)).thenReturn(1);
Assert.assertEquals((int) counters.get(TEST_KEY), 1);
Mockito.verify(counters).get(TEST_KEY);
}
@Test public void testCount2() {
Mockito.when(counters.get(TEST_KEY)).thenReturn(2);
Assert.assertEquals((int) counters.get(TEST_KEY), 2);
Mockito.verify(counters).get(TEST_KEY);
}
Verification fails since
mock is set up for the
whole test.
(use e.g. BeforeMethod)
23
25. Can we write a test that is
both DAMP and DRY?
The answer is Yes!
25
26. Test Data Builder
public class UserProfileInputBuilder {
private String firstName;
private String lastName;
public UserProfileInputBuilder() {
this.firstName = "ftest";
this.lastName = "ltest";
...
}
public UserProfileInputBuilder withFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public UserProfileInputBuilder withLastName(String lastName) {
this.lastName = lastName;
return this;
}
...
public UserProfile build() {
return new UserProfile(this.firstName,
this.lastName);
}
}
26
27. How to use it
EmailValidator emailValidatorMock = Mockito.mock(EmailValidator.class);
LocalizeName localizerMock = Mockito.mock(LocalizeName.class);
ProfileService sut = new ProfileService(emailValidatorMock, localizerMock);
UserProfile invalidInput =
new UserProfileInputBuilder()
.withEmail("invalid email format")
.build();
Mockito.when(emailValidator.validate(invalidInput.getEmail()).thenReturn(false));
//Exercise system and expect InvalidInputException
//Note that create user is suppose to throw an exception when the email is invalid
//We do not need to provide noise and create locale, firstName, and lastName
//for this test case.
sut.createUser(invalidInput);
27
28. Test Data Providers
@DataProvider(name = "drinks")
public Object[][] getDrinks() {
return new Object[][] {
{ "Whiskey", true },
{ "Daiquiri", false }
};
}
@Test(dataProvider = "drinks")
public void testDrinkingService(
String drink,
boolean shouldPass) {
Assert.assertEquals(service.isGood(drink), shouldPass);
}
28