Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Fact and Theory? - xUnit.net

I'm new to xUnit.net and AutoFixture.

I'm currently working on a "testproject" to get familiar with xUnit.net and Autofixture. There is one little thing I don't understand.

What is the difference between [Fact] and [Theory, AutoMoqData]?

Could you please tell me if the following two pieces of code are equal? I'm asking this because the Test succeeds with both, but I want to learn it the right way.

[Fact] public void UpdateVersionWillUpdateCorrectlyInRepository() {     var fixture = new Fixture().Customize(new AutoMoqCustomization());     var contract = fixture.Create<VersionContract>();     var version = fixture.Create<Version>();      fixture.Freeze<Mock<IContractMapper>>()         .Setup(r => r.Map(contract)).Returns(version);      var repMock = fixture.Freeze<Mock<VersionRepository>>();      var sut = fixture.Create<VersionManagementService>();      sut.UpdateVersion(contract);      repMock.Verify(r => r.UpdateVersion(version)); } 

and

[Theory, AutoMoqData] public void UpdateVersionWillUpdateCorrectlyInRepository(     VersionContract contract,     Version version,     [Frozen]Mock<IContractMapper> mapMock,     [Frozen]Mock<VersionRepository> repMock,     VersionManagementService sut) {     mapMock.Setup(r => r.Map(contract)).Returns(version);      sut.UpdateVersion(contract);      repMock.Verify(r => r.UpdateVersion(version)); } 

What makes me think that there is a difference are the Keywords [Fact] and [Theory].

I'm assuming that the [Theory] Keywords tells the xUnit.net framework that the supplied data comes from somewhere, where somewhere is Autofixture. Whereas [Fact] tells xUnit nothing about the origin of the data and I need to build the objects manually.

like image 434
sternze Avatar asked Mar 13 '14 08:03

sternze


People also ask

What is Fact in xUnit test?

xUnit uses the [Fact] attribute to denote a parameterless unit test, which tests invariants in your code. In contrast, the [Theory] attribute denotes a parameterised test that is true for a subset of data. That data can be supplied in a number of ways, but the most common is with an [InlineData] attribute.

What is InlineData in xUnit?

[InlineData] allows us to specify that a separate test is run on a particular Theory method for each instance of [InlineData] .

What is the difference between MSTest and xUnit?

MSTest is concerned, the biggest difference between xUnit and the other two test frameworks (NUnit and MSTest) is that xUnit is much more extensible when compared to NUnit and MSTest. The [Fact] attribute is used instead of the [Test] attribute.

What xUnit test type should you use to test an invariant condition?

Note that xUnit.net supports two types of unit tests: facts and theories. While facts are used to test invariant conditions, theories are tests that are true for a particular set of data passed as argument to the method. You would typically use the [Fact] attribute to write unit tests that have no method arguments.


1 Answers

Assuming that your [AutoMoqData] attribute looks something like this:

public class AutoMoqDataAttribute : AutoDataAttribute {     internal AutoMoqDataAttribute()         : base(new Fixture().Customize(new AutoMoqCustomization()))     {     } } 

Then, yes, those two tests are equivalent.

Both [Fact] and [Theory] attributes are defined by xUnit.net.

The [Fact] attribute is used by the xUnit.net test runner to identify a 'normal' unit test: a test method that takes no method arguments.

The [Theory] attribute, on the other, expects one or more DataAttribute instances to supply the values for a Parameterized Test's method arguments.

xUnit.net itself supplies various attributes that derive from DataAttribute: [InlineData], [ClassData], [PropertyData].

AutoFixture hooks into this extensibility point of xUnit.net by supplying the [AutoData] attribute. It can be used to make tests more declarative.

like image 75
Mark Seemann Avatar answered Oct 06 '22 00:10

Mark Seemann