EFFECTIVE .NET CORE UNIT TESTING WITH SQLITE AND DAPPER Mike Melusky – Philly.NET September 19, 2018
ABOUT SPEAKER • Michael Melusky • Software Developer for Audacious Inquiry in Baltimore, MD • Adjunct Instructor at Penn State University
TOPICS • Motivation for this talk • Apply testing to a “real world” . Netcore C# example • Closing thoughts
CODE FOR TONIGHT • I have a starter project available on Github: • https://github.com/mrjavascript/phillynet-sept2018-meetup • All code for tonight will be pushed to this repository • Slides on SlideShare (mrjavascript)
WHY UNIT TESTING?
STACK OVERFLOW DEVELOPER SURVEY • Each year Stack Overflow polls developers, asking questions to gauge popular languages and frameworks and technologies • Jetbrains (the company behind IntelliJ and Resharper) started publishing their own survey as well • There was an alarming survey response to a question regarding unit testing in the Jetbrains survey
WHAT IS UNIT TESTING? • Testing individual units of code • Numerous frameworks are available for this: • Java: • jUnit / Mockito • .NET: • xUnit, nUnit, mstest, Moq • Javascript: • Jest, Cucumber, Mocha, Enzyme, Protractor … (many, many, many, many more)
JAVA MAKES IT EASY! • Spring Framework recommends having two separate source trees, one for “main” and one for “test” • No excuse to not do unit testing
WHAT ABOUT .NET? • About me, I’m a “Java Developer” by trade • Code is written in Java 8 with Spring Boot 2 • Production Database is PostGreSQL 10 • Uses jUnit and Mockito for testing frameworks
BUILDING A RESTFUL API • For building a RESTful API or backend application, generally the application is layered into the following components: • Controller – handles HTTP request and responses from the clients • Service – applies transactional business logic and validation • Repository – data access layer (SQL or ORM like Hibernate or Entity Framework)
JAVA UNIT TESTS • As mentioned, the live data exists in a production PostGreSQL database. • For the unit tests, the same code is ran against a different in memory database (in this case H2) • I recently had to build a .NET Core 2 application which connects to Microsoft SQL Server • I wanted to apply the same testing strategies used with .NET (data access layer code queries SQL Server in production, but uses a in memory database for tests)
XYZ WIDGET CORPORATION
XYZ WIDGET CORPORATION • Assume we recently were hired to work for XYZ Widget Corporation • Our first task as an employee is to find a way to integrate unit testing into a new .NET Core application another developer wrote • ** DEMO **
.NET UNIT TESTING FRAMEWORKS • The following options are available for writing unit tests with .NET Core • Nunit • MsBuild • Xunit • I’m using Xunit for these examples (personal preference)
N U N I T E X A M P L E
LETS INTEGRATE XUNIT INTO CODE • Before we do this, we need an in-memory database to run the unit tests against • For these examples, let’s use SQLite • ** DEMO **
UNIT TESTING BEST PRACTICES
TEST DRIVEN DEVELOPMENT • A paradigm where the unit tests are written before the application code • .NET Core makes this easy! • This might be a utopia for some applications however…
BEST PRACTICES • Run code coverage reports! • Make unit tests quick so developers get into the habit of running tests before pushing code • If you receive a JIRA ticket for a bug fix, write unit tests to verify your changes! • ** DEMO CODE COVERAGE **
EXAMPLE TEST CASE – 3RD PARTY API
REFERENCES • Stack Overflow Developer Survey 2018 - https://insights.stackoverflow.com/survey/2018/ • Jetbrains Developer Survey - https://www.jetbrains.com/research/devecosystem-2018/ • SQLite & SQL Server Compact Toolbox extension for Visual Studio - https://github.com/ErikEJ/SqlCeToolbox
THANK YOU FOR COMING! • @mrjavascript on Twitter/GitHub/SlideShare…

Effective .NET Core Unit Testing with SQLite and Dapper

  • 1.
    EFFECTIVE .NET COREUNIT TESTING WITH SQLITE AND DAPPER Mike Melusky – Philly.NET September 19, 2018
  • 2.
    ABOUT SPEAKER • MichaelMelusky • Software Developer for Audacious Inquiry in Baltimore, MD • Adjunct Instructor at Penn State University
  • 3.
    TOPICS • Motivation forthis talk • Apply testing to a “real world” . Netcore C# example • Closing thoughts
  • 4.
    CODE FOR TONIGHT •I have a starter project available on Github: • https://github.com/mrjavascript/phillynet-sept2018-meetup • All code for tonight will be pushed to this repository • Slides on SlideShare (mrjavascript)
  • 5.
  • 6.
    STACK OVERFLOW DEVELOPERSURVEY • Each year Stack Overflow polls developers, asking questions to gauge popular languages and frameworks and technologies • Jetbrains (the company behind IntelliJ and Resharper) started publishing their own survey as well • There was an alarming survey response to a question regarding unit testing in the Jetbrains survey
  • 8.
    WHAT IS UNITTESTING? • Testing individual units of code • Numerous frameworks are available for this: • Java: • jUnit / Mockito • .NET: • xUnit, nUnit, mstest, Moq • Javascript: • Jest, Cucumber, Mocha, Enzyme, Protractor … (many, many, many, many more)
  • 9.
    JAVA MAKES IT EASY! •Spring Framework recommends having two separate source trees, one for “main” and one for “test” • No excuse to not do unit testing
  • 10.
    WHAT ABOUT .NET? •About me, I’m a “Java Developer” by trade • Code is written in Java 8 with Spring Boot 2 • Production Database is PostGreSQL 10 • Uses jUnit and Mockito for testing frameworks
  • 11.
    BUILDING A RESTFULAPI • For building a RESTful API or backend application, generally the application is layered into the following components: • Controller – handles HTTP request and responses from the clients • Service – applies transactional business logic and validation • Repository – data access layer (SQL or ORM like Hibernate or Entity Framework)
  • 12.
    JAVA UNIT TESTS •As mentioned, the live data exists in a production PostGreSQL database. • For the unit tests, the same code is ran against a different in memory database (in this case H2) • I recently had to build a .NET Core 2 application which connects to Microsoft SQL Server • I wanted to apply the same testing strategies used with .NET (data access layer code queries SQL Server in production, but uses a in memory database for tests)
  • 13.
  • 14.
    XYZ WIDGET CORPORATION •Assume we recently were hired to work for XYZ Widget Corporation • Our first task as an employee is to find a way to integrate unit testing into a new .NET Core application another developer wrote • ** DEMO **
  • 15.
    .NET UNIT TESTINGFRAMEWORKS • The following options are available for writing unit tests with .NET Core • Nunit • MsBuild • Xunit • I’m using Xunit for these examples (personal preference)
  • 16.
    N U NI T E X A M P L E
  • 17.
    LETS INTEGRATE XUNITINTO CODE • Before we do this, we need an in-memory database to run the unit tests against • For these examples, let’s use SQLite • ** DEMO **
  • 18.
  • 19.
    TEST DRIVEN DEVELOPMENT • Aparadigm where the unit tests are written before the application code • .NET Core makes this easy! • This might be a utopia for some applications however…
  • 20.
    BEST PRACTICES • Runcode coverage reports! • Make unit tests quick so developers get into the habit of running tests before pushing code • If you receive a JIRA ticket for a bug fix, write unit tests to verify your changes! • ** DEMO CODE COVERAGE **
  • 21.
    EXAMPLE TEST CASE– 3RD PARTY API
  • 22.
    REFERENCES • Stack OverflowDeveloper Survey 2018 - https://insights.stackoverflow.com/survey/2018/ • Jetbrains Developer Survey - https://www.jetbrains.com/research/devecosystem-2018/ • SQLite & SQL Server Compact Toolbox extension for Visual Studio - https://github.com/ErikEJ/SqlCeToolbox
  • 23.
    THANK YOU FORCOMING! • @mrjavascript on Twitter/GitHub/SlideShare…