using Conjecture.Core;
using Conjecture.Xunit;
namespace MyProject.Tests;
public class MathTests
{
[Property]
public bool Addition_is_commutative(int a, int b)
{
return a + b == b + a;
}
[Property]
public bool Abs_is_non_negative(int value)
{
// Skip int.MinValue — Math.Abs throws for it
Assume.That(value != int.MinValue);
return Math.Abs(value) >= 0;
}
}
using Conjecture.Core;
using Conjecture.Xunit.V3;
namespace MyProject.Tests;
public class MathTests
{
[Property]
public bool Addition_is_commutative(int a, int b)
{
return a + b == b + a;
}
[Property]
public bool Abs_is_non_negative(int value)
{
// Skip int.MinValue — Math.Abs throws for it
Assume.That(value != int.MinValue);
return Math.Abs(value) >= 0;
}
}
using Conjecture.Core;
using Conjecture.NUnit;
namespace MyProject.Tests;
public class MathTests
{
[Property]
public bool Addition_is_commutative(int a, int b)
{
return a + b == b + a;
}
[Property]
public bool Abs_is_non_negative(int value)
{
// Skip int.MinValue — Math.Abs throws for it
Assume.That(value != int.MinValue);
return Math.Abs(value) >= 0;
}
}
using Conjecture.Core;
using Conjecture.MSTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MyProject.Tests;
[TestClass]
public class MathTests
{
[Property]
public bool Addition_is_commutative(int a, int b)
{
return a + b == b + a;
}
[Property]
public bool Abs_is_non_negative(int value)
{
// Skip int.MinValue — Math.Abs throws for it
Assume.That(value != int.MinValue);
return Math.Abs(value) >= 0;
}
}
using Conjecture.Core;
using Conjecture.TestingPlatform;
namespace MyProject.Tests;
public class MathTests
{
[Property]
public bool Addition_is_commutative(int a, int b)
{
return a + b == b + a;
}
[Property]
public bool Abs_is_non_negative(int value)
{
// Skip int.MinValue — Math.Abs throws for it
Assume.That(value != int.MinValue);
return Math.Abs(value) >= 0;
}
}
3. Run
dotnet test
Each [Property] method runs 100 times with randomly generated inputs. If a property fails, you'll see the shrunk counterexample in the test output.
4. See Shrinking in Action
Write a property that fails:
[Property]
public bool All_ints_are_small(int value)
{
return value < 1000;
}
Run it, and Conjecture will find a counterexample and shrink it down to 1000 — the smallest integer that violates the property.
Return Types
Property methods can return bool (Conjecture asserts it's true) or void (use your framework's assertions):
Control how parameters are generated with [From<T>]:
public class PositiveInts : IStrategyProvider<int>
{
public Strategy<int> Create() => Strategy.Integers<int>(1, int.MaxValue);
}
[Property]
public bool Positive_ints_are_positive([From<PositiveInts>] int value)
{
return value > 0;
}