public void TestHasTimeoutAndExceeds() { string code = @" using System; using System.Threading; using Xunit; public class Stub { [Fact(Timeout = 50)] public void TestShouldTimeout() { Thread.Sleep(120); Assert.Equal(2, 2); } } "; XmlNode assemblyNode = Execute(code); XmlNode testNode = ResultXmlUtility.AssertResult(assemblyNode, "Fail", "Stub.TestShouldTimeout"); XmlNode messageNode = testNode.SelectSingleNode("failure/message"); Assert.Equal("Test execution time exceeded: 50ms", messageNode.InnerText); }
public void TestClassIsNotInstantiatedForSkippedTests() { string code = @" using System; using Xunit; public class MockTestClass { public MockTestClass() { throw new Exception(""Should not reach me!""); } [Fact(Skip = ""the reason"")] public void TestThatShouldBeSkipped() { } } "; XmlNode assemblyNode = Execute(code); XmlNode testNode = ResultXmlUtility.AssertResult(assemblyNode, "Skip", "MockTestClass.TestThatShouldBeSkipped"); XmlNode messageNode = testNode.SelectSingleNode("reason/message"); Assert.Equal("the reason", messageNode.InnerText); }
public void Async40AcceptanceTest() { string code = @" using System; using System.Threading; using System.Threading.Tasks; using Xunit; public class TestClass { [Fact] public Task TestMethod() { return Task.Factory.StartNew(() => { Thread.Sleep(1); }) .ContinueWith(_ => { Assert.True(false); }); } } "; XmlNode assemblyNode = Execute(code); XmlNode testNode = ResultXmlUtility.AssertResult(assemblyNode, "Fail", "TestClass.TestMethod"); XmlNode failureNode = testNode.SelectSingleNode("failure"); ResultXmlUtility.AssertAttribute(failureNode, "exception-type", typeof(TrueException).FullName); }
public void ValueFromUserSpecifiedConfigFile() { string code = @" using System; using System.Configuration; using Xunit; public class MockTestClass { [Fact] public void CheckConfigurationFileEntry() { Assert.Equal(ConfigurationSettings.AppSettings[""ConfigurationValue""], ""42""); } } "; XmlNode assemblyNode; using (MockAssembly mockAssembly = new MockAssembly(assemblyFileName)) { mockAssembly.Compile(code, null); assemblyNode = mockAssembly.Run(configFile); } ResultXmlUtility.AssertResult(assemblyNode, "Pass", "MockTestClass.CheckConfigurationFileEntry"); Assert.Equal(Path.GetFullPath(configFile), assemblyNode.Attributes["configFile"].Value); }
public void TheoryViaClassAcceptanceTest() { string code = @" using System.Collections; using System.Collections.Generic; using Xunit.Extensions; public class StubData : IEnumerable<object[]> { public IEnumerator<object[]> GetEnumerator() { yield return new object[] { 1, ""hello world"", 2.3 }; } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } public class Stub { [Theory, ClassData(typeof(StubData))] public void PassingTestData(int foo, string bar, double baz) { } } "; XmlNode assemblyNode = Execute(code, null, "xunit.extensions.dll"); ResultXmlUtility.AssertResult(assemblyNode, "Pass", "Stub." + @"PassingTestData(foo: 1, bar: ""hello world"", baz: 2.3)"); }
public void VerifiesConstructorIsCalled() { string code = @" using System; using Xunit; public class MockTestClass { int counter; public MockTestClass() { counter++; } [Fact] public void CounterShouldBeIncrementedInConstructor() { Assert.Equal(counter, 1); } } "; XmlNode assemblyNode = Execute(code); ResultXmlUtility.AssertResult(assemblyNode, "Pass", "MockTestClass.CounterShouldBeIncrementedInConstructor"); }
public void TraceAssertDoesNotCreateAnyOutput() { string code = @" #define TRACE using System; using System.Diagnostics; using Xunit; public class TestClass { [Fact] public void SampleTest() { Trace.Assert(false); } } "; XmlNode assemblyNode = Execute(code); XmlNode node = ResultXmlUtility.AssertResult(assemblyNode, "Fail", "TestClass.SampleTest"); Assert.Null(node.SelectSingleNode("output")); }
public void ClassWithStaticTest() { string code = @" using System; using Xunit; public class StaticTests { [Fact] public static void StaticTestMethod() { } }"; XmlNode assemblyNode = Execute(code); ResultXmlUtility.AssertResult(assemblyNode, "Pass", "StaticTests.StaticTestMethod"); }
public void LackOfConfigurationFileBugInCLR4() // http://xunit.codeplex.com/workitem/9696 { string code = @" using System; using Xunit; public class ConfigurationFileExample { [Fact] public void Test() { new Uri(""http://localhost:58080/indexes/categoriesByName?query=CategoryName%3ABeverages&start=0&pageSize=25""); } }"; XmlNode assemblyNode = Execute(code); ResultXmlUtility.AssertResult(assemblyNode, "Pass", "ConfigurationFileExample.Test"); }
public void FactMethodsCannotHaveArguments() { string code = @" using System; using Xunit; public class MockTestClass { [Fact] public void FactWithParameters(int x) { } } "; XmlNode assemblyNode = Execute(code); XmlNode testNode = ResultXmlUtility.AssertResult(assemblyNode, "Fail", "MockTestClass.FactWithParameters"); XmlNode failureNode = testNode.SelectSingleNode("failure"); ResultXmlUtility.AssertAttribute(failureNode, "exception-type", typeof(InvalidOperationException).FullName); Assert.Equal("System.InvalidOperationException : Fact method MockTestClass.FactWithParameters cannot have parameters", failureNode.SelectSingleNode("message").InnerText); }
public void AssertAreEqualTwoNumbersEqualShouldBePassedResult() { string code = @" using System; using Xunit; public class MockTestClass { [Fact] public void SuccessTest() { Assert.Equal(2, 2); } } "; XmlNode assemblyNode = Execute(code); ResultXmlUtility.AssertResult(assemblyNode, "Pass", "MockTestClass.SuccessTest"); }
public void ThrowsExpectedException() { string code = @" using System; using Xunit; public class MockTestClass { [Fact] public void ExpectTest() { Assert.Throws<InvalidOperationException>(delegate { throw new InvalidOperationException(); }); } } "; XmlNode assemblyNode = Execute(code); ResultXmlUtility.AssertResult(assemblyNode, "Pass", "MockTestClass.ExpectTest"); }
public void ValueFromUserSpecifiedConfigFile() { string code = @" using System; using System.Configuration; using Xunit; public class MockTestClass { [Fact] public void CheckConfigurationFileEntry() { Assert.Equal(ConfigurationSettings.AppSettings[""ConfigurationValue""], ""42""); } } "; XmlNode assemblyNode = Execute(code, configFile); ResultXmlUtility.AssertResult(assemblyNode, "Pass", "MockTestClass.CheckConfigurationFileEntry"); }
public void ShouldNotBeExcutingInTheSameAppDomain() { string codeTemplate = @" using System; using System.Diagnostics; using Xunit; public class AppDomainTest {{ [Fact] public void TestDomainName() {{ Assert.False({0} == AppDomain.CurrentDomain.Id); }} }}"; string code = String.Format(codeTemplate, AppDomain.CurrentDomain.Id); XmlNode assemblyNode = Execute(code); ResultXmlUtility.AssertResult(assemblyNode, "Pass", "AppDomainTest.TestDomainName"); }
public void AssertAreEqualTwoNumbersNotEqualShouldThrowException() { string code = @" using System; using Xunit; public class MockTestClass { [Fact] public void TwoNumbersAreNotEqual() { Assert.Equal(2, 3); } } "; XmlNode assemblyNode = Execute(code); XmlNode testNode = ResultXmlUtility.AssertResult(assemblyNode, "Fail", "MockTestClass.TwoNumbersAreNotEqual"); XmlNode failureNode = testNode.SelectSingleNode("failure"); ResultXmlUtility.AssertAttribute(failureNode, "exception-type", typeof(EqualException).FullName); }
public void CurrentDirectoryWhenRunningTestsIsTestAssemblyPath() { string code = @" using System.IO; using Xunit; public class ChangeDirectoryTests { [Fact] public void ChangeDirectory() { string tempPath = Path.GetFullPath(Path.GetTempPath()).TrimEnd('\\'); string currentPath = Path.GetFullPath(Directory.GetCurrentDirectory()).TrimEnd('\\'); Assert.Equal(tempPath, currentPath); } } "; string assemblyName = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); XmlNode assemblyNode = ExecuteWithCustomAssemblyName(code, assemblyName); ResultXmlUtility.AssertResult(assemblyNode, "Pass", "ChangeDirectoryTests.ChangeDirectory"); }
public void TimingForFailedTestShouldReflectActualRunTime() { string code = @" using System; using Xunit; public class MockTestClass { [Fact] public void TwoNumbersAreNotEqual() { System.Threading.Thread.Sleep(100); Assert.Equal(2, 3); } } "; XmlNode assemblyNode = Execute(code); XmlNode testNode = ResultXmlUtility.AssertResult(assemblyNode, "Fail", "MockTestClass.TwoNumbersAreNotEqual"); Assert.NotEqual("0.000", testNode.Attributes["time"].Value); }
public void TheoryViaPropertyAcceptanceTest() { string code = @" using System.Collections.Generic; using Xunit.Extensions; public class Stub { public static IEnumerable<object[]> MyTestData { get { yield return new object[] { 1, ""hello world"", 2.3 }; } } [Theory, PropertyData(""MyTestData"")] public void PassingTestData(int foo, string bar, double baz) { } } "; XmlNode assemblyNode = Execute(code, null, "xunit.extensions.dll"); ResultXmlUtility.AssertResult(assemblyNode, "Pass", "Stub." + @"PassingTestData(foo: 1, bar: ""hello world"", baz: 2.3)"); }
public void TestIsMarkedSkippedAndDoesNotExecute() { string code = @" using System; using Xunit; public class MockTestClass { [Fact(Skip = ""the reason"")] public void FailedTestThatShouldBeSkipped() { Assert.Equal(2, 3); } } "; XmlNode assemblyNode = Execute(code); XmlNode testNode = ResultXmlUtility.AssertResult(assemblyNode, "Skip", "MockTestClass.FailedTestThatShouldBeSkipped"); XmlNode messageNode = testNode.SelectSingleNode("reason/message"); Assert.Equal("the reason", messageNode.InnerText); }
public void UsesCorrectExternalRunner() { string code = @" using System; using System.Collections.Generic; using System.Reflection; using Xunit; using Xunit.Sdk; namespace Test.AcceptanceTest.ExternalRunner { public class StubExternalRunner : ITestClassCommand { ITypeInfo typeUnderTest; public object ObjectUnderTest { get { return null; } } public ITypeInfo TypeUnderTest { get { return typeUnderTest; } set { typeUnderTest = value; } } public Exception ClassFinish() { return null; } public Exception ClassStart() { return null; } public int ChooseNextTest(ICollection<IMethodInfo> methods) { return 0; } public IEnumerable<ITestCommand> EnumerateTestCommands(IMethodInfo testMethod) { yield return new StubRunnerTestCommand(testMethod); } public IEnumerable<IMethodInfo> EnumerateTestMethods() { yield return Reflector.Wrap(typeof(ClassUnderTest).GetMethod(""Method"")); } public bool IsTestMethod(IMethodInfo testMethod) { return testMethod.Name == ""Method""; } } public class StubRunnerTestCommand : TestCommand { public StubRunnerTestCommand(IMethodInfo method) : base(method, null, 0) { } public override MethodResult Execute(object testClass) { return new PassedResult(testMethod, DisplayName); } } [RunWith(typeof(StubExternalRunner))] public class ClassUnderTest { public void Method() {} } } "; XmlNode assemblyNode = Execute(code); ResultXmlUtility.AssertResult(assemblyNode, "Pass", "Test.AcceptanceTest.ExternalRunner.ClassUnderTest.Method"); }