John Louderback
John Louderback

Reputation: 99

How To Unit Test Multiple Instances Of Class In xUnit

I'm fairly new to unit testing. I understand the concept, but I keep find myself bumping into walls with the execution. How, in xUnit, do I test multiple instances of a class? For instance, say I have a constructor with a signature:

public FileWatcher(string path = "", bool startWatching = true)

I want to run all of my unit tests for each public property and method that I'm testing against multiple instances of FileWatcher, each with the constructor passed different arguments. This way I can run all of my tests against an instance of FileWatcher where no arguments were passed in, and then one where arguments were. I don't know if what I'm trying to do reeks of code smell or if I don't know the correct nomenclature to turn up any results, but I can't find anything on this subject.

Upvotes: 0

Views: 1923

Answers (1)

Janci
Janci

Reputation: 51

I believe, you should think about your approach. From the signature, I think your FileWatcher should/would/does behave differently when path is null, when file exists, when the file does not exist.

There is not much point in testing the same scenario over and over, because you're basically running the same test.

You should try to find different edge cases and ideally create one test for each.

That said, it is possible to create Theories in xUnit, these are tests with parameters. I would suggest xUnit Theory: Working With InlineData, MemberData, ClassData, this helped me.

Upvotes: 1

Related Questions