How to use FactAttribute method of Xunit.v3.Mocks class

Best Xunit code snippet using Xunit.v3.Mocks.FactAttribute

Mocks.cs

Source:Mocks.cs Github

copy

Full Screen

...120 testMethod,121 message122 );123 }124 public static IReflectionAttributeInfo FactAttribute(125 string? displayName = null,126 string? skip = null,127 int timeout = 0)128 {129 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();130 result.Attribute.Returns(new FactAttribute { DisplayName = displayName, Skip = skip, Timeout = timeout });131 result.GetNamedArgument<string>("DisplayName").Returns(displayName);132 result.GetNamedArgument<string>("Skip").Returns(skip);133 result.GetNamedArgument<int>("Timeout").Returns(timeout);134 return result;135 }136 static IEnumerable<IAttributeInfo> LookupAttribute(137 string fullyQualifiedTypeName,138 IReflectionAttributeInfo[]? attributes)139 {140 if (attributes == null)141 return Enumerable.Empty<IAttributeInfo>();142 var attributeType = Type.GetType(fullyQualifiedTypeName);143 if (attributeType == null)144 return Enumerable.Empty<IAttributeInfo>();145 return attributes.Where(attribute => attributeType.IsAssignableFrom(attribute.Attribute.GetType())).ToList();146 }147 public static IMethodInfo MethodInfo(148 string? methodName = null,149 IReflectionAttributeInfo[]? attributes = null,150 IParameterInfo[]? parameters = null,151 ITypeInfo? type = null,152 ITypeInfo? returnType = null,153 bool isAbstract = false,154 bool isPublic = true,155 bool isStatic = false)156 {157 var result = Substitute.For<IMethodInfo, InterfaceProxy<IMethodInfo>>();158 attributes ??= new IReflectionAttributeInfo[0];159 parameters ??= new IParameterInfo[0];160 result.IsAbstract.Returns(isAbstract);161 result.IsPublic.Returns(isPublic);162 result.IsStatic.Returns(isStatic);163 result.Name.Returns(methodName ?? "method:" + Guid.NewGuid().ToString("n"));164 result.ReturnType.Returns(returnType);165 result.Type.Returns(type);166 result.GetCustomAttributes("").ReturnsForAnyArgs(callInfo => LookupAttribute(callInfo.Arg<string>(), attributes));167 result.GetParameters().Returns(parameters);168 return result;169 }170 public static IParameterInfo ParameterInfo(string name)171 {172 var result = Substitute.For<IParameterInfo, InterfaceProxy<IParameterInfo>>();173 result.Name.Returns(name);174 return result;175 }176 public static IReflectionMethodInfo ReflectionMethodInfo<TClass>(string methodName)177 {178 return Reflector.Wrap(typeof(TClass).GetMethod(methodName)!);179 }180 public static IReflectionTypeInfo ReflectionTypeInfo<TClass>()181 {182 return Reflector.Wrap(typeof(TClass));183 }184 public static IRunnerReporter RunnerReporter(185 string? runnerSwitch = null,186 string? description = null,187 bool isEnvironmentallyEnabled = false,188 IMessageSinkWithTypes? messageSink = null)189 {190 var result = Substitute.For<IRunnerReporter, InterfaceProxy<IRunnerReporter>>();191 result.Description.Returns(description ?? "The runner reporter description");192 result.IsEnvironmentallyEnabled.ReturnsForAnyArgs(isEnvironmentallyEnabled);193 result.RunnerSwitch.Returns(runnerSwitch);194 var dualSink = MessageSinkAdapter.Wrap(messageSink ?? Substitute.For<IMessageSinkWithTypes, InterfaceProxy<IMessageSinkWithTypes>>());195 result.CreateMessageHandler(null!).ReturnsForAnyArgs(dualSink);196 return result;197 }198 public static ITest Test(199 ITestCase testCase,200 string displayName)201 {202 var result = Substitute.For<ITest, InterfaceProxy<ITest>>();203 result.DisplayName.Returns(displayName);204 result.TestCase.Returns(testCase);205 return result;206 }207 public static ITestAssembly TestAssembly(IReflectionAttributeInfo[] attributes)208 {209 var assemblyInfo = AssemblyInfo(attributes: attributes);210 var result = Substitute.For<ITestAssembly, InterfaceProxy<ITestAssembly>>();211 result.Assembly.Returns(assemblyInfo);212 return result;213 }214 public static ITestAssembly TestAssembly(215 string assemblyFileName,216 string? configFileName = null,217 ITypeInfo[]? types = null,218 IReflectionAttributeInfo[]? attributes = null)219 {220 var assemblyInfo = AssemblyInfo(types, attributes, assemblyFileName);221 var result = Substitute.For<ITestAssembly, InterfaceProxy<ITestAssembly>>();222 result.Assembly.Returns(assemblyInfo);223 result.ConfigFileName.Returns(configFileName);224 return result;225 }226 public static TestAssembly TestAssembly(227 Assembly? assembly = null,228 string? configFileName = null)229 {230#if NETFRAMEWORK231 if (configFileName == null)232 configFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;233#endif234 return new TestAssembly(Reflector.Wrap(assembly ?? typeof(Mocks).Assembly), configFileName);235 }236 public static ITestAssemblyDiscoveryFinished TestAssemblyDiscoveryFinished(237 bool diagnosticMessages = false,238 int toRun = 42,239 int discovered = 2112)240 {241 var assembly = new XunitProjectAssembly { AssemblyFilename = "testAssembly.dll", ConfigFilename = "testAssembly.dll.config" };242 var config = new TestAssemblyConfiguration { DiagnosticMessages = diagnosticMessages, ShadowCopy = true };243 var result = Substitute.For<ITestAssemblyDiscoveryFinished, InterfaceProxy<ITestAssemblyDiscoveryFinished>>();244 result.Assembly.Returns(assembly);245 result.DiscoveryOptions.Returns(TestFrameworkOptions.ForDiscovery(config));246 result.TestCasesDiscovered.Returns(discovered);247 result.TestCasesToRun.Returns(toRun);248 return result;249 }250 public static ITestAssemblyDiscoveryStarting TestAssemblyDiscoveryStarting(251 bool diagnosticMessages = false,252 AppDomainOption appDomain = AppDomainOption.Disabled,253 bool shadowCopy = false)254 {255 var assembly = new XunitProjectAssembly { AssemblyFilename = "testAssembly.dll", ConfigFilename = "testAssembly.dll.config" };256 var config = new TestAssemblyConfiguration { DiagnosticMessages = diagnosticMessages, MethodDisplay = Xunit.TestMethodDisplay.ClassAndMethod, MaxParallelThreads = 42, ParallelizeTestCollections = true, ShadowCopy = shadowCopy };257 var result = Substitute.For<ITestAssemblyDiscoveryStarting, InterfaceProxy<ITestAssemblyDiscoveryStarting>>();258 result.AppDomain.Returns(appDomain);259 result.Assembly.Returns(assembly);260 result.DiscoveryOptions.Returns(TestFrameworkOptions.ForDiscovery(config));261 result.ShadowCopy.Returns(shadowCopy);262 return result;263 }264 public static ITestAssemblyExecutionFinished TestAssemblyExecutionFinished(265 bool diagnosticMessages = false,266 int total = 2112,267 int failed = 42,268 int skipped = 8,269 int errors = 6,270 decimal time = 123.456M)271 {272 var assembly = new XunitProjectAssembly { AssemblyFilename = "testAssembly.dll", ConfigFilename = "testAssembly.dll.config" };273 var config = new TestAssemblyConfiguration { DiagnosticMessages = diagnosticMessages, ShadowCopy = true };274 var summary = new ExecutionSummary { Total = total, Failed = failed, Skipped = skipped, Errors = errors, Time = time };275 var result = Substitute.For<ITestAssemblyExecutionFinished, InterfaceProxy<ITestAssemblyExecutionFinished>>();276 result.Assembly.Returns(assembly);277 result.ExecutionOptions.Returns(TestFrameworkOptions.ForExecution(config));278 result.ExecutionSummary.Returns(summary);279 return result;280 }281 public static ITestAssemblyExecutionStarting TestAssemblyExecutionStarting(282 bool diagnosticMessages = false,283 string? assemblyFilename = null)284 {285 var assembly = new XunitProjectAssembly { AssemblyFilename = assemblyFilename ?? "testAssembly.dll", ConfigFilename = "testAssembly.dll.config" };286 var config = new TestAssemblyConfiguration { DiagnosticMessages = diagnosticMessages, MethodDisplay = Xunit.TestMethodDisplay.ClassAndMethod, MaxParallelThreads = 42, ParallelizeTestCollections = true, ShadowCopy = true };287 var result = Substitute.For<ITestAssemblyExecutionStarting, InterfaceProxy<ITestAssemblyExecutionStarting>>();288 result.Assembly.Returns(assembly);289 result.ExecutionOptions.Returns(TestFrameworkOptions.ForExecution(config));290 return result;291 }292 public static ITestAssemblyFinished TestAssemblyFinished(293 int testsRun = 2112,294 int testsFailed = 42,295 int testsSkipped = 6,296 decimal executionTime = 123.4567M)297 {298 var testAssembly = TestAssembly("testAssembly.dll");299 var result = Substitute.For<ITestAssemblyFinished, InterfaceProxy<ITestAssemblyFinished>>();300 result.TestAssembly.Returns(testAssembly);301 result.TestsRun.Returns(testsRun);302 result.TestsFailed.Returns(testsFailed);303 result.TestsSkipped.Returns(testsSkipped);304 result.ExecutionTime.Returns(executionTime);305 return result;306 }307 public static ITestAssemblyStarting TestAssemblyStarting()308 {309 var testAssembly = TestAssembly("testAssembly.dll");310 var result = Substitute.For<ITestAssemblyStarting, InterfaceProxy<ITestAssemblyStarting>>();311 result.TestAssembly.Returns(testAssembly);312 return result;313 }314 public static ITestCase TestCase(ITestCollection? collection = null)315 {316 if (collection == null)317 collection = TestCollection();318 var result = Substitute.For<ITestCase, InterfaceProxy<ITestCase>>();319 result.TestMethod.TestClass.TestCollection.Returns(collection);320 return result;321 }322 public static ITestCase TestCase(ITestMethod testMethod)323 {324 var result = Substitute.For<ITestCase, InterfaceProxy<ITestCase>>();325 result.TestMethod.Returns(testMethod);326 return result;327 }328 public static ITestCase TestCase<TClassUnderTest>(329 string methodName,330 string? displayName = null,331 string? skipReason = null,332 string? uniqueID = null,333 string? fileName = null,334 int? lineNumber = null)335 {336 return TestCase(typeof(TClassUnderTest), methodName, displayName, skipReason, uniqueID, fileName, lineNumber);337 }338 public static ITestCase TestCase(339 Type type,340 string methodName,341 string? displayName = null,342 string? skipReason = null,343 string? uniqueID = null,344 string? fileName = null,345 int? lineNumber = null)346 {347 var testMethod = TestMethod(type, methodName);348 var traits = GetTraits(testMethod.Method);349 var result = Substitute.For<ITestCase, InterfaceProxy<ITestCase>>();350 result.DisplayName.Returns(displayName ?? $"{type}.{methodName}");351 result.SkipReason.Returns(skipReason);352 result.TestMethod.Returns(testMethod);353 result.Traits.Returns(traits);354 result.UniqueID.Returns(uniqueID ?? Guid.NewGuid().ToString());355 if (fileName != null && lineNumber != null)356 {357 var sourceInfo = new Xunit.SourceInformation { FileName = fileName, LineNumber = lineNumber };358 result.SourceInformation.Returns(sourceInfo);359 }360 return result;361 }362 public static IReflectionAttributeInfo TestCaseOrdererAttribute(string typeName, string assemblyName)363 {364 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();365 result.Attribute.Returns(new TestCaseOrdererAttribute(typeName, assemblyName));366 result.GetConstructorArguments().Returns(new object[] { typeName, assemblyName });367 return result;368 }369 public static IReflectionAttributeInfo TestCaseOrdererAttribute(Type type)370 {371 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();372 result.Attribute.Returns(new TestCaseOrdererAttribute(type));373 result.GetConstructorArguments().Returns(new object[] { type });374 return result;375 }376 public static IReflectionAttributeInfo TestCaseOrdererAttribute<TOrderer>() =>377 TestCaseOrdererAttribute(typeof(TOrderer));378 public static ITestClass TestClass(379 string? typeName = null,380 IReflectionAttributeInfo[]? attributes = null)381 {382 var testCollection = TestCollection();383 var typeInfo = TypeInfo(typeName, attributes: attributes);384 var result = Substitute.For<ITestClass, InterfaceProxy<ITestClass>>();385 result.Class.Returns(typeInfo);386 result.TestCollection.Returns(testCollection);387 return result;388 }389 public static TestClass TestClass(390 Type type,391 ITestCollection? collection = null)392 {393 if (collection == null)394 collection = TestCollection(type.Assembly);395 return new TestClass(collection, Reflector.Wrap(type));396 }397 public static TestCollection TestCollection(398 Assembly? assembly = null,399 ITypeInfo? definition = null,400 string? displayName = null)401 {402 if (assembly == null)403 assembly = typeof(Mocks).Assembly;404 if (displayName == null)405 displayName = "Mock test collection for " + assembly.CodeBase;406 return new TestCollection(TestAssembly(assembly), definition, displayName);407 }408 public static ITestCollectionFinished TestCollectionFinished(409 string displayName = "Display Name",410 int testsRun = 2112,411 int testsFailed = 42,412 int testsSkipped = 6,413 decimal executionTime = 123.4567M)414 {415 var result = Substitute.For<ITestCollectionFinished, InterfaceProxy<ITestCollectionFinished>>();416 result.TestsRun.Returns(testsRun);417 result.TestsFailed.Returns(testsFailed);418 result.TestsSkipped.Returns(testsSkipped);419 result.ExecutionTime.Returns(executionTime);420 result.TestCollection.DisplayName.Returns(displayName);421 return result;422 }423 public static ITestCollectionStarting TestCollectionStarting()424 {425 var result = Substitute.For<ITestCollectionStarting, InterfaceProxy<ITestCollectionStarting>>();426 result.TestCollection.DisplayName.Returns("Display Name");427 return result;428 }429 public static IReflectionAttributeInfo TestCollectionOrdererAttribute(string typeName, string assemblyName)430 {431 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();432 result.Attribute.Returns(new TestCollectionOrdererAttribute(typeName, assemblyName));433 result.GetConstructorArguments().Returns(new object[] { typeName, assemblyName });434 return result;435 }436 public static IReflectionAttributeInfo TestCollectionOrdererAttribute(Type type)437 {438 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();439 result.Attribute.Returns(new TestCollectionOrdererAttribute(type));440 result.GetConstructorArguments().Returns(new object[] { type });441 return result;442 }443 public static IReflectionAttributeInfo TestCollectionOrdererAttribute<TOrderer>() =>444 TestCollectionOrdererAttribute(typeof(TOrderer));445 public static ITestFailed TestFailed(446 Type type,447 string methodName,448 string? displayName = null,449 string? output = null,450 decimal executionTime = 0M,451 Exception? ex = null)452 {453 var testCase = TestCase(type, methodName);454 var test = Test(testCase, displayName ?? "NO DISPLAY NAME");455 var failureInfo = Xunit.Sdk.ExceptionUtility.ConvertExceptionToFailureInformation(ex ?? new Exception());456 var result = Substitute.For<ITestFailed, InterfaceProxy<ITestFailed>>();457 result.ExceptionParentIndices.Returns(failureInfo.ExceptionParentIndices);458 result.ExceptionTypes.Returns(failureInfo.ExceptionTypes);459 result.ExecutionTime.Returns(executionTime);460 result.Messages.Returns(failureInfo.Messages);461 result.Output.Returns(output);462 result.StackTraces.Returns(failureInfo.StackTraces);463 result.TestCase.Returns(testCase);464 result.Test.Returns(test);465 return result;466 }467 public static ITestFailed TestFailed(468 string displayName,469 decimal executionTime,470 string? exceptionType = null,471 string? exceptionMessage = null,472 string? stackTrace = null,473 string? output = null)474 {475 var testCase = TestCase();476 var test = Test(testCase, displayName);477 var result = Substitute.For<ITestFailed, InterfaceProxy<ITestFailed>>();478 result.ExceptionParentIndices.Returns(new[] { -1 });479 result.ExceptionTypes.Returns(new[] { exceptionType });480 result.ExecutionTime.Returns(executionTime);481 result.Messages.Returns(new[] { exceptionMessage });482 result.Output.Returns(output);483 result.StackTraces.Returns(new[] { stackTrace });484 result.TestCase.Returns(testCase);485 result.Test.Returns(test);486 return result;487 }488 public static IReflectionAttributeInfo TestFrameworkAttribute(Type type)489 {490 var attribute = Activator.CreateInstance(type);491 if (attribute == null)492 throw new InvalidOperationException($"Unable to create attribute instance: '{type.FullName}'");493 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();494 result.Attribute.Returns(attribute);495 result.GetCustomAttributes(null).ReturnsForAnyArgs(496 callInfo => LookupAttribute(497 callInfo.Arg<string>(),498 CustomAttributeData.GetCustomAttributes(attribute.GetType()).Select(x => Reflector.Wrap(x)).ToArray()499 )500 );501 return result;502 }503 public static ITestMethod TestMethod(504 string? typeName = null,505 string? methodName = null,506 string? displayName = null,507 string? skip = null,508 int timeout = 0,509 IEnumerable<IParameterInfo>? parameters = null,510 IEnumerable<IReflectionAttributeInfo>? classAttributes = null,511 IEnumerable<IReflectionAttributeInfo>? methodAttributes = null)512 {513 if (classAttributes == null)514 classAttributes = Enumerable.Empty<IReflectionAttributeInfo>();515 if (methodAttributes == null)516 methodAttributes = Enumerable.Empty<IReflectionAttributeInfo>();517 if (parameters == null)518 parameters = Enumerable.Empty<IParameterInfo>();519 var factAttribute = methodAttributes.FirstOrDefault(attr => typeof(FactAttribute).IsAssignableFrom(attr.Attribute.GetType()));520 if (factAttribute == null)521 {522 factAttribute = FactAttribute(displayName, skip, timeout);523 methodAttributes = methodAttributes.Concat(new[] { factAttribute });524 }525 var testClass = TestClass(typeName, attributes: classAttributes.ToArray());526 var methodInfo = MethodInfo(methodName, methodAttributes.ToArray(), parameters.ToArray(), testClass.Class);527 var result = Substitute.For<ITestMethod, InterfaceProxy<ITestMethod>>();528 result.Method.Returns(methodInfo);529 result.TestClass.Returns(testClass);530 return result;531 }532 public static TestMethod TestMethod(533 Type type,534 string methodName,535 ITestCollection? collection = null)536 {...

Full Screen

Full Screen

TheoryDiscovererTests.cs

Source:TheoryDiscovererTests.cs Github

copy

Full Screen

...71 {72 discoveryOptions.SetPreEnumerateTheories(true);73 var discoverer = TestableTheoryDiscoverer.Create();74 var testMethod = Mocks.TestMethod(typeof(MultipleDataClass), "TheoryMethod");75 var factAttribute = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).Single();76 var testCases = discoverer.Discover(discoveryOptions, testMethod, factAttribute).ToList();77 Assert.Equal(2, testCases.Count);78 Assert.Single(testCases, testCase => testCase.DisplayName == "TheoryDiscovererTests+MultipleDataClass.TheoryMethod(x: 42)");79 Assert.Single(testCases, testCase => testCase.DisplayName == "TheoryDiscovererTests+MultipleDataClass.TheoryMethod(x: 2112)");80 }81 [Fact]82 public void DiscoveryOptions_PreEnumerateTheoriesSetToFalse_YieldsSingleTheoryTestCase()83 {84 discoveryOptions.SetPreEnumerateTheories(false);85 var discoverer = TestableTheoryDiscoverer.Create();86 var testMethod = Mocks.TestMethod(typeof(MultipleDataClass), "TheoryMethod");87 var factAttribute = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).Single();88 var testCases = discoverer.Discover(discoveryOptions, testMethod, factAttribute);89 var testCase = Assert.Single(testCases);90 var theoryTestCase = Assert.IsType<XunitTheoryTestCase>(testCase);91 Assert.Equal("TheoryDiscovererTests+MultipleDataClass.TheoryMethod", theoryTestCase.DisplayName);92 }93 class MultipleDataAttribute : DataAttribute94 {95 public override IEnumerable<object[]> GetData(MethodInfo testMethod)96 {97 yield return new object[] { 42 };98 yield return new object[] { 2112 };99 }100 }101 class MultipleDataClass102 {103 [Theory, MultipleDataAttribute]104 public void TheoryMethod(int x) { }105 }106 [Fact]107 public void DiscoverOptions_PreEnumerateTheoriesSetToTrueWithSkipOnData_YieldsSkippedTestCasePerDataRow()108 {109 discoveryOptions.SetPreEnumerateTheories(true);110 var discoverer = TestableTheoryDiscoverer.Create();111 var testMethod = Mocks.TestMethod(typeof(MultipleDataClassSkipped), "TheoryMethod");112 var factAttribute = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).Single();113 var testCases = discoverer.Discover(discoveryOptions, testMethod, factAttribute).ToList();114 Assert.Equal(2, testCases.Count);115 Assert.Single(testCases, testCase => testCase.DisplayName == "TheoryDiscovererTests+MultipleDataClassSkipped.TheoryMethod(x: 42)" && testCase.SkipReason == "Skip this attribute");116 Assert.Single(testCases, testCase => testCase.DisplayName == "TheoryDiscovererTests+MultipleDataClassSkipped.TheoryMethod(x: 2112)" && testCase.SkipReason == "Skip this attribute");117 }118 class MultipleDataClassSkipped119 {120 [Theory, MultipleDataAttribute(Skip = "Skip this attribute")]121 public void TheoryMethod(int x) { }122 }123 [Fact]124 public void ThrowingData()125 {126 var discoverer = TestableTheoryDiscoverer.Create();127 var testMethod = Mocks.TestMethod(typeof(ThrowingDataClass), "TheoryWithMisbehavingData");128 var factAttribute = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).Single();129 var testCases = discoverer.Discover(discoveryOptions, testMethod, factAttribute);130 var testCase = Assert.Single(testCases);131 var theoryTestCase = Assert.IsType<XunitTheoryTestCase>(testCase);132 Assert.Equal("TheoryDiscovererTests+ThrowingDataClass.TheoryWithMisbehavingData", theoryTestCase.DisplayName);133 var message = Assert.Single(discoverer.DiagnosticMessages);134 var diagnostic = Assert.IsAssignableFrom<IDiagnosticMessage>(message);135 Assert.StartsWith($"Exception thrown during theory discovery on 'TheoryDiscovererTests+ThrowingDataClass.TheoryWithMisbehavingData'; falling back to single test case.{Environment.NewLine}System.DivideByZeroException: Attempted to divide by zero.", diagnostic.Message);136 }137 class ThrowingDataAttribute : DataAttribute138 {139 public override IEnumerable<object[]> GetData(MethodInfo method)140 {141 throw new DivideByZeroException();142 }143 }144 class ThrowingDataClass145 {146 [Theory, ThrowingData]147 public void TheoryWithMisbehavingData(string a) { }148 }149 [Fact]150 public void DataDiscovererReturningNullYieldsSingleTheoryTestCase()151 {152 var discoverer = TestableTheoryDiscoverer.Create();153 var theoryAttribute = Mocks.TheoryAttribute();154 var dataAttribute = Mocks.DataAttribute();155 var testMethod = Mocks.TestMethod("MockTheoryType", "MockTheoryMethod", methodAttributes: new[] { theoryAttribute, dataAttribute });156 var testCases = discoverer.Discover(discoveryOptions, testMethod, theoryAttribute);157 var testCase = Assert.Single(testCases);158 var theoryTestCase = Assert.IsType<XunitTheoryTestCase>(testCase);159 Assert.Equal("MockTheoryType.MockTheoryMethod", theoryTestCase.DisplayName);160 }161 [Fact]162 public void NonSerializableDataYieldsSingleTheoryTestCase()163 {164 var discoverer = TestableTheoryDiscoverer.Create();165 var testMethod = Mocks.TestMethod(typeof(NonSerializableDataClass), "TheoryMethod");166 var factAttribute = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).Single();167 var testCases = discoverer.Discover(discoveryOptions, testMethod, factAttribute);168 var testCase = Assert.Single(testCases);169 var theoryTestCase = Assert.IsType<XunitTheoryTestCase>(testCase);170 Assert.Equal("TheoryDiscovererTests+NonSerializableDataClass.TheoryMethod", theoryTestCase.DisplayName);171 var message = Assert.Single(discoverer.DiagnosticMessages);172 var diagnostic = Assert.IsAssignableFrom<IDiagnosticMessage>(message);173 Assert.Equal("Non-serializable data ('System.Object[]') found for 'TheoryDiscovererTests+NonSerializableDataClass.TheoryMethod'; falling back to single test case.", diagnostic.Message);174 }175 class NonSerializableDataAttribute : DataAttribute176 {177 public override IEnumerable<object[]> GetData(MethodInfo method)178 {179 yield return new object[] { 42 };180 yield return new object[] { new NonSerializableDataAttribute() };181 }182 }183 class NonSerializableDataClass184 {185 [Theory, NonSerializableData]186 public void TheoryMethod(object a) { }187 }188#if NETFRAMEWORK189 [Fact]190 public void TheoryWithNonSerializableEnumYieldsSingleTheoryTestCase()191 {192 Assert.SkipUnless(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), "The GAC is only available on Windows");193 var discoverer = TestableTheoryDiscoverer.Create();194 var testMethod = Mocks.TestMethod(typeof(NonSerializableEnumDataClass), "TheTest");195 var factAttribute = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).Single();196 var testCases = discoverer.Discover(discoveryOptions, testMethod, factAttribute);197 var testCase = Assert.Single(testCases);198 var theoryTestCase = Assert.IsType<XunitTheoryTestCase>(testCase);199 Assert.Equal("TheoryDiscovererTests+NonSerializableEnumDataClass.TheTest", theoryTestCase.DisplayName);200 }201 class NonSerializableEnumDataClass202 {203 [Theory]204 [InlineData(42)]205 [InlineData(ConformanceLevel.Auto)]206 public void TheTest(object x) { }207 }208#endif209 [Fact]210 public async void NoSuchDataDiscoverer_ThrowsInvalidOperationException()211 {212 var results = await RunAsync<ITestFailed>(typeof(NoSuchDataDiscovererClass));213 var failure = Assert.Single(results);214 Assert.Equal("System.InvalidOperationException", failure.ExceptionTypes.Single());215 Assert.Equal("Data discoverer specified for TheoryDiscovererTests+NoSuchDataDiscovererAttribute on TheoryDiscovererTests+NoSuchDataDiscovererClass.Test does not exist.", failure.Messages.Single());216 }217 class NoSuchDataDiscovererClass218 {219 [Theory]220 [NoSuchDataDiscoverer]221 public void Test() { }222 }223 [DataDiscoverer("Foo.Blah.ThingDiscoverer", "invalid_assembly_name")]224 public class NoSuchDataDiscovererAttribute : DataAttribute225 {226 public override IEnumerable<object[]> GetData(MethodInfo testMethod)227 {228 throw new NotImplementedException();229 }230 }231 [Fact]232 public async void NotADataDiscoverer_ThrowsInvalidOperationException()233 {234 var results = await RunAsync<ITestFailed>(typeof(NotADataDiscovererClass));235 var failure = Assert.Single(results);236 Assert.Equal("System.InvalidOperationException", failure.ExceptionTypes.Single());237 Assert.Equal("Data discoverer specified for TheoryDiscovererTests+NotADataDiscovererAttribute on TheoryDiscovererTests+NotADataDiscovererClass.Test does not implement IDataDiscoverer.", failure.Messages.Single());238 }239 class NotADataDiscovererClass240 {241 [Theory]242 [NotADataDiscoverer]243 public void Test() { }244 }245 [DataDiscoverer(typeof(TheoryDiscovererTests))]246 public class NotADataDiscovererAttribute : DataAttribute247 {248 public override IEnumerable<object[]> GetData(MethodInfo testMethod)249 {250 throw new NotImplementedException();251 }252 }253 [Fact]254 public void NonDiscoveryEnumeratedDataYieldsSingleTheoryTestCase()255 {256 var discoverer = TestableTheoryDiscoverer.Create();257 var testMethod = Mocks.TestMethod(typeof(NonDiscoveryEnumeratedData), "TheoryMethod");258 var factAttribute = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).Single();259 var testCases = discoverer.Discover(discoveryOptions, testMethod, factAttribute);260 var testCase = Assert.Single(testCases);261 var theoryTestCase = Assert.IsType<XunitTheoryTestCase>(testCase);262 Assert.Equal("TheoryDiscovererTests+NonDiscoveryEnumeratedData.TheoryMethod", theoryTestCase.DisplayName);263 }264 class NonDiscoveryEnumeratedData265 {266 public static IEnumerable<object[]> foo { get { return Enumerable.Empty<object[]>(); } }267 public static IEnumerable<object[]> bar { get { return Enumerable.Empty<object[]>(); } }268 [Theory]269 [MemberData("foo", DisableDiscoveryEnumeration = true)]270 [MemberData("bar", DisableDiscoveryEnumeration = true)]271 public static void TheoryMethod(int x) { }272 }273 [Fact]274 public void MixedDiscoveryEnumerationDataYieldSingleTheoryTestCase()275 {276 var discoverer = TestableTheoryDiscoverer.Create();277 var testMethod = Mocks.TestMethod(typeof(MixedDiscoveryEnumeratedData), "TheoryMethod");278 var factAttribute = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).Single();279 var testCases = discoverer.Discover(discoveryOptions, testMethod, factAttribute);280 var testCase = Assert.Single(testCases);281 var theoryTestCase = Assert.IsType<XunitTheoryTestCase>(testCase);282 Assert.Equal("TheoryDiscovererTests+MixedDiscoveryEnumeratedData.TheoryMethod", theoryTestCase.DisplayName);283 }284 class MixedDiscoveryEnumeratedData285 {286 public static IEnumerable<object[]> foo { get { return Enumerable.Empty<object[]>(); } }287 public static IEnumerable<object[]> bar { get { return Enumerable.Empty<object[]>(); } }288 [Theory]289 [MemberData("foo", DisableDiscoveryEnumeration = false)]290 [MemberData("bar", DisableDiscoveryEnumeration = true)]291 public static void TheoryMethod(int x) { }292 }...

Full Screen

Full Screen

Mocks.Attributes.cs

Source:Mocks.Attributes.cs Github

copy

Full Screen

...81 result.Attribute.Returns(dataAttribute);82 result.GetNamedArgument<string?>("Skip").Returns(skip);83 return result;84 }85 public static _IReflectionAttributeInfo FactAttribute(86 string? displayName = null,87 string? skip = null,88 int timeout = 0)89 {90 var result = Substitute.For<_IReflectionAttributeInfo, InterfaceProxy<_IReflectionAttributeInfo>>();91 result.Attribute.Returns(new FactAttribute { DisplayName = displayName, Skip = skip, Timeout = timeout });92 result.GetNamedArgument<string?>("DisplayName").Returns(displayName);93 result.GetNamedArgument<string?>("Skip").Returns(skip);94 result.GetNamedArgument<int>("Timeout").Returns(timeout);95 return result;96 }97 static IReadOnlyCollection<_IReflectionAttributeInfo> LookupAttribute(98 string fullyQualifiedTypeName,99 _IReflectionAttributeInfo[]? attributes)100 {101 if (attributes == null)102 return EmptyAttributeInfos;103 var attributeType = Type.GetType(fullyQualifiedTypeName);104 if (attributeType == null)105 return EmptyAttributeInfos;...

Full Screen

Full Screen

XunitTestCaseTests.cs

Source:XunitTestCaseTests.cs Github

copy

Full Screen

...6using Xunit.Sdk;7using Xunit.v3;8public class XunitTestCaseTests9{10 public class FactAttributeValues11 {12 [Fact]13 public static void DisplayName()14 {15 var factAttribute = Mocks.FactAttribute(displayName: "Custom Display Name");16 var testMethod = Mocks.TestMethod(methodAttributes: new[] { factAttribute });17 var testCase = new TestableXunitTestCase(testMethod);18 Assert.Equal("Custom Display Name", testCase.TestCaseDisplayName);19 }20 [Fact]21 public static void DisplayNameWithArguments()22 {23 var factAttribute = Mocks.FactAttribute(displayName: "Custom Display Name");24 var param1 = Mocks.ParameterInfo("p1");25 var param2 = Mocks.ParameterInfo("p2");26 var param3 = Mocks.ParameterInfo("p3");27 var testMethod = Mocks.TestMethod(methodAttributes: new[] { factAttribute }, parameters: new[] { param1, param2, param3 });28 var arguments = new object[] { 42, "Hello, world!", 'A' };29 var testCase = new TestableXunitTestCase(testMethod, arguments);30 Assert.Equal("Custom Display Name(p1: 42, p2: \"Hello, world!\", p3: 'A')", testCase.TestCaseDisplayName);31 }32 [Fact]33 public static void SkipReason()34 {35 var factAttribute = Mocks.FactAttribute(skip: "Skip Reason");36 var testMethod = Mocks.TestMethod(methodAttributes: new[] { factAttribute });37 var testCase = new TestableXunitTestCase(testMethod);38 Assert.Equal("Skip Reason", testCase.SkipReason);39 }40 [Fact]41 public static void Timeout()42 {43 var factAttribute = Mocks.FactAttribute(timeout: 42);44 var testMethod = Mocks.TestMethod(methodAttributes: new[] { factAttribute });45 var testCase = new TestableXunitTestCase(testMethod);46 Assert.Equal(42, testCase.Timeout);47 }48 }49 public class Traits : AcceptanceTestV350 {51 [Fact]52 public static void TraitsOnTestMethod()53 {54 var trait1 = Mocks.TraitAttribute("Trait1", "Value1");55 var trait2 = Mocks.TraitAttribute("Trait2", "Value2");56 var testMethod = Mocks.TestMethod(methodAttributes: new[] { trait1, trait2 });57 var testCase = new TestableXunitTestCase(testMethod);...

Full Screen

Full Screen

Mocks.TestCases.cs

Source:Mocks.TestCases.cs Github

copy

Full Screen

...165 {166 parameters ??= EmptyParameterInfos;167 classAttributes ??= EmptyAttributeInfos;168 methodAttributes ??= EmptyAttributeInfos;169 // Ensure that there's a FactAttribute, or else it's not technically a test method170 var factAttribute = methodAttributes.FirstOrDefault(attr => typeof(FactAttribute).IsAssignableFrom(attr.Attribute.GetType()));171 if (factAttribute == null)172 {173 factAttribute = FactAttribute(displayName, skip, timeout);174 methodAttributes = methodAttributes.Concat(new[] { factAttribute }).ToArray();175 }176 var testClass = TestClass(typeName, attributes: classAttributes, collection: collection);177 var methodInfo = MethodInfo(methodName, methodAttributes, parameters, testClass.Class);178 return TestMethod(methodInfo, testClass, uniqueID);179 }180 public static _ITestMethod TestMethod<TClassUnderTest>(181 string methodName,182 _ITestCollection? collection = null,183 string? uniqueID = null) =>184 TestMethod(MethodInfo<TClassUnderTest>(methodName), TestClass<TClassUnderTest>(collection), uniqueID);185 public static _ITestMethod TestMethod(186 _IMethodInfo methodInfo,187 _ITestClass testClass,...

Full Screen

Full Screen

FactDiscovererTests.cs

Source:FactDiscovererTests.cs Github

copy

Full Screen

...15 public FactDiscovererTests()16 {17 aggregator = new ExceptionAggregator();18 cancellationTokenSource = new CancellationTokenSource();19 factAttribute = Mocks.FactAttribute();20 messageBus = new SpyMessageBus();21 options = _TestFrameworkOptions.ForDiscovery();22 }23 [Fact]24 public async void FactWithoutParameters_ReturnsTestCaseThatRunsFact()25 {26 var discoverer = new FactDiscoverer();27 var testMethod = Mocks.TestMethod<ClassUnderTest>("FactWithNoParameters");28 var testCases = await discoverer.Discover(options, testMethod, factAttribute);29 var testCase = Assert.Single(testCases);30 await testCase.RunAsync(messageBus, new object[0], aggregator, cancellationTokenSource);31 Assert.Single(messageBus.Messages.OfType<_TestPassed>());32 }33 [Fact]...

Full Screen

Full Screen

FactAttribute

Using AI Code Generation

copy

Full Screen

1using Xunit;2using Xunit.v3;3{4 public void TestMethod()5 {6 Assert.Equal(1, 1);7 }8}9using Xunit;10using Xunit.v3;11{12 public void TestMethod()13 {14 Assert.Equal(1, 1);15 }16}17using Xunit;18using Xunit.v3;19{20 public void TestMethod()21 {22 Assert.Equal(1, 1);23 }24}25using Xunit;26using Xunit.v3;27{28 public void TestMethod()29 {30 Assert.Equal(1, 1);31 }32}33using Xunit;34using Xunit.v3;35{36 public void TestMethod()37 {38 Assert.Equal(1, 1);39 }40}41using Xunit;42using Xunit.v3;43{44 public void TestMethod()45 {46 Assert.Equal(1, 1);47 }48}49using Xunit;50using Xunit.v3;51{52 public void TestMethod()53 {54 Assert.Equal(1, 1);55 }56}57using Xunit;58using Xunit.v3;59{60 public void TestMethod()61 {62 Assert.Equal(1, 1);63 }64}65using Xunit;66using Xunit.v3;67{

Full Screen

Full Screen

FactAttribute

Using AI Code Generation

copy

Full Screen

1using Xunit.v3.Mocks;2using Xunit.v2.Mocks;3using Xunit.v3.Mocks;4using Xunit.v2.Mocks;5using Xunit.v3.Mocks;6using Xunit.v2.Mocks;7using Xunit.v3.Mocks;8using Xunit.v2.Mocks;9using Xunit.v3.Mocks;10using Xunit.v2.Mocks;11using Xunit.v3.Mocks;12using Xunit.v2.Mocks;13using Xunit.v3.Mocks;14using Xunit.v2.Mocks;15using Xunit.v3.Mocks;16using Xunit.v2.Mocks;17using Xunit.v3.Mocks;18using Xunit.v2.Mocks;19using Xunit.v3.Mocks;20using Xunit.v2.Mocks;21using Xunit.v3.Mocks;22using Xunit.v2.Mocks;23using Xunit.v3.Mocks;24using Xunit.v2.Mocks;25using Xunit.v3.Mocks;26using Xunit.v2.Mocks;27using Xunit.v3.Mocks;28using Xunit.v2.Mocks;29using Xunit.v3.Mocks;30using Xunit.v2.Mocks;31using Xunit.v3.Mocks;32using Xunit.v2.Mocks;33using Xunit.v3.Mocks;34using Xunit.v2.Mocks;35using Xunit.v3.Mocks;36using Xunit.v2.Mocks;37using Xunit.v3.Mocks;38using Xunit.v2.Mocks;39using Xunit.v3.Mocks;40using Xunit.v2.Mocks;41using Xunit.v3.Mocks;42using Xunit.v2.Mocks;43using Xunit.v3.Mocks;44using Xunit.v2.Mocks;45using Xunit.v3.Mocks;46using Xunit.v2.Mocks;

Full Screen

Full Screen

FactAttribute

Using AI Code Generation

copy

Full Screen

1using Xunit;2using Xunit.v3;3{4 {5 public void Test1()6 {7 var mock = new Mocks();8 var mockFact = mock.Fact("Test1");9 var factAttribute = mockFact.GetAttribute<FactAttribute>();10 Assert.NotNull(factAttribute);11 }12 }13}

Full Screen

Full Screen

FactAttribute

Using AI Code Generation

copy

Full Screen

1using Xunit;2using Xunit.v3;3{4 {5 public void TestMethod1()6 {7 var mock = Mocks.FactAttribute(() => true);8 Assert.True(mock.IsFact);9 }10 }11}

Full Screen

Full Screen

FactAttribute

Using AI Code Generation

copy

Full Screen

1using Xunit.v3;2using Xunit;3using Xunit.Abstractions;4using Xunit.Sdk;5using System;6using System.Collections.Generic;7using System.Linq;8using System.Reflection;9using System.Text;10using System.Threading.Tasks;11using Xunit.Runner.Common;12using Xunit.Runner.v2;13using Xunit.Runner.v2.Discovery;14using Xunit.Runner.v2.Execution;15using Xunit.Runner.v2.Tests;16using Xunit.Runner.v2.Tests.Utilities;17{18 {19 public static string FactAttribute(string methodName)20 {21 var testMethod = new TestMethod(typeof(Tests), methodName);22 var factAttribute = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).Single();23 return factAttribute.ToString();24 }25 }26}27using Xunit.v3;28using Xunit;29using Xunit.Abstractions;30using Xunit.Sdk;31using System;32using System.Collections.Generic;33using System.Linq;34using System.Reflection;35using System.Text;36using System.Threading.Tasks;37using Xunit.Runner.Common;38using Xunit.Runner.v2;39using Xunit.Runner.v2.Discovery;40using Xunit.Runner.v2.Execution;41using Xunit.Runner.v2.Tests;42using Xunit.Runner.v2.Tests.Utilities;43{44 {45 public static string FactAttribute(string methodName)46 {47 var testMethod = new TestMethod(typeof(Tests), methodName);48 var factAttribute = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).Single();49 return factAttribute.ToString();50 }51 }52}53using Xunit.v3;54using Xunit;55using Xunit.Abstractions;56using Xunit.Sdk;57using System;58using System.Collections.Generic;59using System.Linq;60using System.Reflection;61using System.Text;62using System.Threading.Tasks;63using Xunit.Runner.Common;64using Xunit.Runner.v2;65using Xunit.Runner.v2.Discovery;66using Xunit.Runner.v2.Execution;67using Xunit.Runner.v2.Tests;68using Xunit.Runner.v2.Tests.Utilities;69{70 {71 public static string FactAttribute(string methodName)72 {73 var testMethod = new TestMethod(typeof(Tests), methodName

Full Screen

Full Screen

FactAttribute

Using AI Code Generation

copy

Full Screen

1using Xunit.v3.Mocks;2using Xunit.v3;3{4 {5 public FactAttribute()6 {7 var fact = Mocks.Fact("Test");8 var testCase = Mocks.TestCase<ITestClass>("TestMethod", "TestProject.TestClass", "TestProject.dll", fact);9 var testMethod = Mocks.TestMethod<ITestClass>("TestMethod", "TestProject.TestClass", "TestProject.dll", new[] { testCase });10 var testClass = Mocks.TestClass("TestProject.TestClass", "TestProject.dll", new[] { testMethod });11 var testAssembly = Mocks.TestAssembly("TestProject.dll", new[] { testClass });12 var discoverySink = Mocks.TestFrameworkDiscoverySink(new[] { testCase });13 var discoveryOptions = Mocks.TestFrameworkOptions();14 Discover(discoverySink, discoveryOptions);15 }16 }17}18using Xunit.v3.Mocks;19using Xunit.v3;20{21 {22 public TestCaseAttribute()23 {24 var fact = Mocks.Fact("Test");25 var testCase = Mocks.TestCase<ITestClass>("TestMethod", "TestProject.TestClass", "TestProject.dll", fact);26 var testMethod = Mocks.TestMethod<ITestClass>("TestMethod", "TestProject.TestClass", "TestProject.dll", new[] { testCase });27 var testClass = Mocks.TestClass("TestProject.TestClass", "TestProject.dll", new[] { testMethod });28 var testAssembly = Mocks.TestAssembly("TestProject.dll", new[] { testClass });29 var discoverySink = Mocks.TestFrameworkDiscoverySink(new[] { testCase });30 var discoveryOptions = Mocks.TestFrameworkOptions();31 Discover(discoverySink, discoveryOptions);32 }33 }34}35using Xunit.v3.Mocks;36using Xunit.v3;37{38 {39 public TestMethodAttribute()40 {41 var fact = Mocks.Fact("Test");42 var testCase = Mocks.TestCase<ITestClass>("TestMethod", "TestProject.TestClass",

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful