Skip to content

Latest commit

 

History

History
71 lines (52 loc) · 3.94 KB

readme.md

File metadata and controls

71 lines (52 loc) · 3.94 KB

Code smell in tests

This presentation was part of the Windows user group days 2020. Purpose of it was to show importance of tests quality and demonstrate how these smells can be improved. As usually each code smell has its own directory with source code. There are two solutions, one with source code smells and one with example solution.

NOTE: The presented solutions don't represent the only solution nor lead to better or shorter code in all cases.

How to collect the code smells

  • The idea was to learn from others mistakes
  • We are unable to calculate statistics to find "The most common mistake", there is no such tool, only static code analysis, which needs to be setup for all studied repositories
  • No one is able to study all repositories with all the source code, because number of lines is huge
  • Picked up only what was obvious and already known (from my own experience)
  • => Ambitions not fulfilled, only collected examples of code smells visible in source code

Checked repositories

To use trustful code examples i didn't base my presentation on my personal experience only. I searched for examples from real world public repositories and why not to pick up the well known?

F.I.R.S.T. Expectations

Unit tests should be:

  • Fast
  • Isolated/Independent
  • Repeatable
  • Self-Validating
  • Timely

Examples

Following examples are shown in format repo:FullClassPath.

  • Multiple asserts (dotnetruntime:System.Reflection.Metadata.HandleTests, dotnetruntime:System.IO.BinaryReaderTests, dotnetruntime:System.Net.WebClientTest)
    • Mono contains Assert messages with index of the assert to be able identify which test fail (mono:MonoTests.System.Windows.Forms.DateTimePickerTest)
  • Test multiple things (dotnetruntime:System.IO.BinaryWriter.WriteByteCharTests)
  • Usage of random (dotnetruntime:System.IO.BinaryReaderTests)
    • DateTime.Now (chocolatey:chocolatey.tests.integration.infrastructure.filesystem.DotNetFileSystemSpecs, System.Tests.DateTimeTests)
  • Magic constants (dotnetruntime:System.IO.BinaryWriterTests)
    • Numeric calculations (dotnetruntime:System.Reflection.Metadata.LargeTablesAndHeapsTests)
  • "IF" Conditional statements (dotnetruntime:System.Security.SecureStringTest)
  • Code duplicity (dotnetruntime:System.Threading.Tasks.CancellationTokenTests)
    • Inheritance usage (mono:MonoTests.System.HttpStyleUriParserTest)

Recommendations

  • Take care of your code the same way you do with your production code
  • Refactor the code together when changing production code
  • The Unit tests are also written in Object oriented language, use the OOP principles also in tests

Side notes

  • Most .Net repositories use xUnit as testing framework
  • xUnit uses it self to test its behavior (as good framework should do)
  • To be able compile .net runtime Python is needed
  • Behavior driven development is used in Chocolatey repo, implemented using TinySpec.NUnit

Links