Assert an Exception using XUnit

I am a newbie to XUnit and Moq. I have a method which takes string as an argument.How to handle an exception using XUnit. [Fact] public void ProfileRepository_GetSettingsForUserIDWithInvalidArguments_ThrowsArgumentException() { //arrange ProfileRepository profiles = new ProfileRepository(); //act var result = profiles.GetSettingsForUserID(“”); //assert //The below statement is not working as expected. Assert.Throws<ArgumentException>(() => profiles.GetSettingsForUserID(“”)); } Method under … Read more

Mock HttpContext.Current in Test Init Method

I’m trying to add unit testing to an ASP.NET MVC application I have built. In my unit tests I use the following code: [TestMethod] public void IndexAction_Should_Return_View() { var controller = new MembershipController(); controller.SetFakeControllerContext(“TestUser”); … } With the following helpers to mock the controller context: public static class FakeControllerContext { public static HttpContextBase FakeHttpContext(string username) … Read more

Disadvantages of Test Driven Development? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for … Read more

Class Not Found: Empty Test Suite in IntelliJ

I’m just starting the computer science program at my college, and I’m having some issues with IntelliJ. When I try to run unit tests, I get the message Process finished with exit code 1 Class not found: “edu.macalester.comp124.hw0.AreaTest”Empty test suite. I also see a message entitled “No tests were found” on the left side of … Read more

How can I disable logging while running unit tests in Python Django?

I am using a simple unit test based test runner to test my Django application. My application itself is configured to use a basic logger in settings.py using: logging.basicConfig(level=logging.DEBUG) And in my application code using: logger = logging.getLogger(__name__) logger.setLevel(getattr(settings, ‘LOG_LEVEL’, logging.DEBUG)) However, when running unittests, I’d like to disable logging so that it doesn’t clutter … Read more

Why can’t code inside unit tests find bundle resources?

Some code I am unit testing needs to load a resource file. It contains the following line: NSString *path = [[NSBundle mainBundle] pathForResource:@”foo” ofType:@”txt”]; In the app it runs just fine, but when run by the unit testing framework pathForResource: returns nil, meaning it could not locate foo.txt. I’ve made sure that foo.txt is included … Read more

Unable to find testhost.dll. Please publish your test project and retry

I have a simple dotnet core class library with a single XUnit test method: TestLib.csproj: <Project Sdk=”Microsoft.NET.Sdk”> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include=”Microsoft.NET.Test.SDK” Version=”15.9.0″ /> <PackageReference Include=”xunit” Version=”2.4.1″ /> <PackageReference Include=”xunit.runner.console” Version=”2.4.1″> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> <PackageReference Include=”xunit.runner.visualstudio” Version=”2.4.1″> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> <PackageReference Include=”xunit.runners” Version=”2.0.0″ /> </ItemGroup> </Project> … Read more

How to unit test with ILogger in ASP.NET Core

This is my controller: public class BlogController : Controller { private IDAO<Blog> _blogDAO; private readonly ILogger<BlogController> _logger; public BlogController(ILogger<BlogController> logger, IDAO<Blog> blogDAO) { this._blogDAO = blogDAO; this._logger = logger; } public IActionResult Index() { var blogs = this._blogDAO.GetMany(); this._logger.LogInformation(“Index page say hello”, new object[0]); return View(blogs); } } As you can see I have 2 … Read more

Why is the Visual Studio 2015/2017/2019 Test Runner not discovering my xUnit v2 tests

Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted. UPDATE: Adding a 2019; the discovery/runner integration mechanism is same as per 2017 & 2015, so the key things that can go wrong are … Read more