Coder Social home page Coder Social logo

keghub / unit-testing-csharp Goto Github PK

View Code? Open in Web Editor NEW
18.0 3.0 8.0 120 KB

This repository contains the source files for the course "Unit Testing in C#". The course is currently available at the wiki of the repository.

Home Page: https://docs.educationsmediagroup.com/unit-testing-csharp/

License: MIT License

unit-testing csharp guide autofixture moq nunit3

unit-testing-csharp's Introduction

Unit Testing in C#

This repository contains the source files for the course "Unit Testing in C#". The course is available here.

This course focuses on the creation of suites of unit tests aiming at asserting the known behavior of .NET components.

It show how NUnit, Moq and AutoFixture work and how to glue them together to quickly create powerful test suites that clearly communicate their intent.

unit-testing-csharp's People

Contributors

kralizek avatar simgu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

unit-testing-csharp's Issues

NUnit Assumptions

It could be a good idea to split assumptions and assertions into separate topics, and also expand some more on when to use assumptions and have some example.

Use TestContext.Progress.Writeline instead of Debug

NUnit requires you to use TestContext.Progress.WriteLine to actually see the output with dotnet test at least in version 3.12 and NUnit3TestAdapter 3.17

nunit/nunit3-vs-adapter#343 (comment)

This was found when going through https://docs.educationsmediagroup.com/unit-testing-csharp/nunit/lifecycle-of-a-test-fixture

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
RootFixtureSetup:OneTimeSetUp
FixtureSetup:OneTimeSetUp
Tests:Constructor
Tests:OneTimeSetUp
Tests:SetUp
Tests:Test1
Tests:TearDown
Tests:SetUp
Tests:Test2
Tests:TearDown
Tests:OneTimeTearDown
FixtureSetup:OneTimeTearDown
RootFixtureSetup:OneTimeTearDown

Passed!  - Failed:     0, Passed:     2, Skipped:     0, Total:     2, Duration: 5 ms - /Users/home/RiderProjects/UnitTesting/UnitTesting/bin/Debug/net5.0/UnitTesting.dll (net5.0)

I can do a PR if you'd like.

Migrate to GitBook

GitBook makes very nice documentation sites and it integrates with GitHub, so we can continue working via PRs here and get our merged changes automatically show up there.

This is the trial I'm running: it's currently based on the gitbook branch but when this issue is solved, it should be based on the master branch.

My plan is to wait for the #20 and #14 to be merged and then proceed with the migration.

Moq - Callbacks - Better ways to alter state

In the Moq - Callbacks topic, at the Problems section the following is mentioned:

Finally, although callbacks can be used to alter the state of the unit test in reaction to a call to a certain method of the mocked type, there are generally better ways to do so and developers should look at callbacks as their last resort.

I think we should expand on why this is a bad idea and what approaches to take instead.

Moq - Quick glance at Moq - Expand on basic concepts

I think that the section Moq - Quick glance at Moq needs to be expanded to fit more junior developers. I think we should expand on the following points:

  • The purpose of mock and what problem it is solving. Maybe also add a link to the Dealing with dependency section.
  • Expand on the Setup method and what it does in the example
  • Expand on the Verify method and what it does in the example

Understanding the Setup and Verify methods are key to understanding the rest of the documentation, so spending more time here and explaining their usage is important for someone who is new to Moq.

Connect the "Arrange - Act - Assert" pattern where possible

It would be good to connect the "Arrange - Act - Assert" pattern to all examples where we have written a full test. As an example; in the NUnit - Asynchronous executions section there are two tests where we could add comments on what lines belong to which part.

We could also see if it's possible to refer to this pattern from some of the other sections, like NUnit - Assertions and mention that we are now looking at the "Assert" part.

AutoFixture - Create page for NUnit 3.0 glue library

This page should explain how this package glues AutoFixture and NUnit 3.0 together.

Topics of the page should be:

  • [AutoData] and [InlineAutoData] attributes
  • How to customize the [AutoData] and [InlineAutoData] attributes

Open for comments.

The second example in AutoFixture type customization page is wrong

The Type Customization section states that AutoFixture lets you override a customization with the Build<T> method:

These type-wide customizations can be overridden by customizing the object creation via Build<T>.

var fixture = new Fixture();

fixture.Customize<Person>(c => c.With(p => p.FirstName, "John")
                              .With(p => p.LastName, "Smith"));

var person = fixture.Build<Person>()
                   .With(p => p.FirstName, "Sam")
                   .Create();

Assert.That(person.FirstName, Is.EqualTo("Sam"));
Assert.That(person.LastName, Is.EqualTo("Smith"));

This is incorrect. The Customize and Build<T> methods are two mutually exclusive ways to customize the creation of a type, meant for different scenarios.

This distinction is explained in the documentation for the Build<T> method:

Note that the Build method chain is best understood as a one-off Customization. It bypasses all Customizations on the Fixture instance. Instead, it allows fine-grained control when building a specific specimen. However, in most cases, adding a convention-based ICustomization is a better, more flexible option.

Data annotation

For the data annotation (or customizations ๐Ÿค” ) it might be worth mentioning NoDataAnnotationsCustomization. I found out about it through this page, if you're interested AutoFixture/AutoFixture#722 .

Moq - Properties - "the Property property"

In the first example, maybe we could rename the Property-property, so that this sentence This will instruct Moq to configure the property so its Property property returns the string "Bar". is a bit easier to read?

Also, should the first "property" in the sentence really be "property"? Shouldn't it be something more like this:
This will instruct Moq to configure the mocked object so its Property property returns the string "Bar".

Is it possible to "Freeze" parameter specifying value?

It is not an issue, but a question. I wonder is it possible to extend frozen attribute and set the value? To have something like this.

    public class TestClass
    {
        public string TestProperty { get; set; }
    }

    [Test]
    public void Test_method([Frozen("someString")] string parameter, TestClass sut)
    {
        Assert.That(sut.TestProperty, Is.EqualTo("someString"));
    }

Fixture - Reference to BCL

In the Fixture section there is a reference to BCL:

This configuration includes the wiring for the different parts of the framework and configurations for the most common types of the BCL.

Either expand on the acronym BCL, add a link to the definition or change this to something easier to comprehend.

Add link to license to footer

Since the repo is public, we need an explicit statement about the license.

I already added a LICENSE file but it's not visible in the wiki.

The idea is to create a footer that links to the LICENSE file.

To create a footer, simply add a _Footer.md file to the docs folder.

NUnit - Assertions - Issues with string property navigation

In the NUnit - Assertion topic, at the section Combining assertions there is the following text:

The problem with this approach is that the combined constraints can only target the same "actual value". There are ways to explore properties of an object but since strings are used to navigate the properties, it can create issues in the future.

I think it would be to either mention what issue it can create or to skip mentioning issues at all.

Link: https://github.com/emgdev/unit-testing-csharp/wiki/Assertions

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.