TESTING TEST DRIVEN DEVELOPMENT

Slides:



Advertisements
Similar presentations
Unit Testing Australian Development Centre Brisbane, Australia.
Advertisements

Practice Session 5 Java: Packages Collection Classes Iterators Generics Design by Contract Test Driven Development JUnit.
J-Unit Framework.
TEST-DRIVEN DEVELOPMENT Lecture 3. Definition Test-driven development (development through testing) is a technique of programming, in which the unit tests.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
CS 2110 Software Design Principles II Based on slides originally by Juan Altmayer Pizzorno port25.com.
JUnit. What is unit testing? A unit is the smallest testable part of an application. A unit test automatically verifies the correctness of the unit. There.
JUnit. Why is testing good? Due to psychological factors, programmers are bad testers. A computer can test much faster than a human Philosophy: “If it.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
Unit Testing Discussion C. Unit Test ● public Method is smallest unit of code ● Input/output transformation ● Test if the method does what it claims ●
Equivalence Partitioning Identify the inputs, behaviors, or other factors that you want to test based on the functionality and specifications Group these.
How to Write Unit Tests in C#
XUnit Test Patterns writing good unit tests Peter Wiles.
CIT 590 Unit testing. Agenda Debugging attempt 2 (because I am stubborn) What is unit testing Why? Unit testing framework in Python.
Programmer Testing Testing all things Java using JUnit and extensions.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
XUnit.net Extensibility Brad Wilson Jim Newkirk. Schedule Assert Extensibility9:00 Before/After Extensibility9:45 Fact Extensibility10:15 BREAK10:45 Fixture.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
BDD with SpecFlow. Why BDD? 1. BDD helps build the right thing. Traditionally there are many handoffs - Requirements, development, component testing,
Unit and Functional Testing Your Flex Applications Mike Nimer Dir. Of Engineering nomee.com.
Testing Especially Unit Testing. V-model Wikipedia:
© ALEXANDRE CUVA  VERSION 2.00 Test Driven Design.
Test Driven Development Arrange, Act, Assert… Awesome Jason Offutt Software Engineer Central Christian Church
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
A Practical Guide To Unit Testing John E. Boal TestDrivenDeveloper.com.
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole.
A tool for test-driven development
CSCA48H Style and Testing. 2 Style The Zen of Python: import this Do the Goodger reading!
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
 Wes McClure  
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
Test Driven Development Introduction Issued date: 8/29/2007 Author: Nguyen Phuc Hai.
Unit Testing with FlexUnit
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
Automated Testing with PHPUnit. How do you know your code works?
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Lecture 5: Test-Driven Development Basics
Software Construction Lab 10 Unit Testing with JUnit
Unit Testing - solid fundamentals
Mocking Tool for easier unit testing
CIT 590 Unit testing.
Unit Testing with xUnit.net
2.7 Inheritance Types of inheritance
Test Driven Development
More JUnit CS 4501 / 6501 Software Testing
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2005
Coding Defensively Coding Defensively
TDD Overview CS 4501/6501 Software Testing
Computer Science 209 Testing With JUnit.
An Automated Testing Framework
Unit testing C# classes
Unit Testing with xUnit.net-Part-2
Test-driven development (TDD)
Introduction to JUnit CS 4501 / 6501 Software Testing
More JUnit CS 4501 / 6501 Software Testing
Tonga Institute of Higher Education
Presentation transcript:

TESTING TEST DRIVEN DEVELOPMENT ASP.NET TESTING TEST DRIVEN DEVELOPMENT Andres Käver, IT Kolledž 2016

TESTING - Intro Developers Managers BUT! I don’t test. I’m a developer. I Create. Managers I don’t want to pay developers to test. That’s why I pay testers. BUT! It costs up to 500% more to fix a bug in production, than to fix it before commiting it to source control

Testing - Benefits Higher quality Living documentation Well-crafted code Automatic regression harness Relative cost of testing Unit Testing – 2 Integration Testing – 6 System Testing – 12 Functional Testing - 12

Testing – Unit test UNIT TEST Much easier in new code (Greenfield) A unit test confirms functionality of a small unit of functionality or component in a larger system Much easier in new code (Greenfield) Harder in legacy code (Brownfield) – code was often not written with testability in mind

Testing - frameworks Nunit – www.nunit.com xUnit – xunit.codeplex.com MSpec - https://github.com/machine/machine.specifications MSTest – included in visual studio

Testing - frameworks

Testing - TDD Same as jig (template) Jig – a shape/form upon which something else is built Jig is the specification for the thing being built Remove the jig after completion In software development, the jig (testsuite) is often built just ahead of the actual code

Testing - TDD Test Driven Development Create a failing test RED Make a test pass GREEN Refactor Improve the internal implementation without changig the external contract or behavior (cleaning while we cook…)

Testing - TDD THE TESTS ARE THE SPEC!!!!! MS Word docs cant prove, that software works

Testing - xUnit Install from NuGet – xUnit.net Add support for Resharper test runner Attributes [Fact] [Theory] [InlineData()] [Trait] Asserting Assert.*****()

Testing – xUnit assert – 3 phases Triple A - AAA Arrange (sut – system under test) Act Assert

Testing – xUnit assert - numeric Assert.****(expected, actual) Numeric results Assert.Equal Floating point values Asset.Equal(expected,actual,precision)

Testing – xUnit assert - strings Assert.Equal Ignore case Assert.Equal(expected, actual, ignoreCase: true) Contains Assert.Contains Starts with, Ends with Assert.StartsWith Assert.EndsWith Regex Assert.Matches(regex, actual)

Testing – xUnit assert – boolean, null Assert.False Assert.True Assert.Null

Testing – xUnit - collection All items in collection Assert.All(collection, lambda_or_method) Assert.All(stringlist, s => Assert.False(s.IsNullOrWhiteSpace(s))); Contains specific item Assert.Contains(item, ListOfItems) Does not contain specific item Assert.DoesNotContain(item, ListOfItems)

Testing – xUnit - collection Satisfying predicate (function that returns boolean) Assert.Containt(ListOfItems, item => item.SomeOperation()) Has all expected items Assert.Equal(expectedList, listToTest)

Testing – xUnit – numeric range Assert.InRange(actual, low, high)

Testing – xUnit - exceptions Assert.Throws<ExceptionTypeExpected>(methodToCall) Assert.Throws<DividedByZeroExcpetion>(() => 5/0) Inspecting exception var ex = Assert.Throws<ExceptionTypeExpected>(methodToCall); Assert.Equal(“parameterName”,ex.ParamName);

Testing – xUnit – object types Exact object type Assert.IsType(typeof(Class), actualObject) Derived object type Assert.IsAssignableFrom(typeof(Class), actualObject) Casting for futher examination var obj = Assert.IsType<ClassToCast>(actualObject) Assert.XXXX(expected, obj.Actual)

Testing – xUnit – test grouping Grouping tests [Trait(“Category”,”SomeCategoryName”)] name-value pairs “Category” – conventional name Can be applied to single test or to whole class Can use several different traits at the same time

Testing – xUnit – diagnostic output Some test frameworks (and xUnit < 2.0) allow to use Console.WriteLine In xUnit 2 you have to use ITestOutputHelper Use Xunit.Abstractions Declare private field in test class private readonly ITestOutputHelper _testOutputHelper; Initialize it in constructor public ClassName(ITestOutputHelper helper){ _testOutputHelper = helper; Use it in place of Console.WriteLine _testOutputHelper.WriteLine(…..)

Testing – xUnit – setup and clean-up Run code before and after each test Constructor for setup (initialize sut) Dispose() for cleanup (implement IDisposable in testclass) xUnit creates new instance of test class for every test in testclass!

Testing – xUnit – test runner option If test runner names are too long Namespace.ProjectName.MethodName Configure app.config <configuration><appsetings> <add key=”xunit.methodDisplay” value=“method”> Will only display method name

Testing – xUnit - testFixture Used, when sut is very expensive/slow to create (ef database for example) Create sut only once and use it in all tests Create separate class to hold your sut (and initialize sut in fixture constructor) (implement IDisposable) Use IClassFixture<YourFixtureClass> in testclass Get and save instance in testclass contructor public ClassName(ITestOutputHelper helper, IClassFixture<YourFixtureClass> fixture){ _fixture = fixture; _fixture.Cleanup() <-!!!!!! Cleanup _fixture.sut in testclasses constructor (implementation is up to you)

Testing – xUnit – sharing fixture Add this attribute to several testclasses [Collection(“SomeCollectionName Collection”)] Remove IClassFixture<YourFixtureClass> from testclasses Create custom collection class Use CollectionDefinition(“SomeCollectionName Collection”) attribute Implemet ICollectionFixture<YourFixtureClass>

Testing – xUnit – data driven tests A single test method that can be executed multiple times, each time using a discreet set of test data, with each execution representing an individual test case. Reduced test code duplication Reduced maintenance cost Add new test cases quickly External test cases (database, csv, …)

Testing – xUnit – data driven tests Use [Theory] and [InlineData] attribute [Theory] [InlineData(10,0)] [InlineData(20,2)] public void ShouldDivideByZero(int a, int b){ Assert.Throws<DividedByZeroExcpetion>(() => a/b) } Unlimited count of parameters and InlineData attributes

Testing – xUnit – data driven test [MemberData(“propertyName”)] propertyName – public static field public static IENumberable<object[]> propertyName{ get{ yield return new object[] {10,0}; yield return new object[] {20,2}; } } [Theory] [MemberData(“propertyName”)] public void ShouldDivideByZero(int a, int b){ Assert.Throws<DividedByZeroExcpetion>(() => a/b) }

Testing – xUnit – data driven test Remove data to separate class Use attribute on test method [MemberData(“propertyName”, MemberType = typeof(SeparateClass))]

Testing – xUnit – data driven test Creating custom data source attributes Inherit from DataAttribute Implement GetData method Use custom attribute in test method Anonymous AutoFixture test data supplied to data driven tests [AutoData] [InlineAutoData] http://github.com/AutoFixture/AutoFixture