EzDevInfo.com

unit-testing interview questions

Top unit-testing frequently asked interview questions

How do I use Assert to verify that an exception has been thrown?

How do I use Assert (or other Test class?) to verify that an exception has been thrown?


Source: (StackOverflow)

How do you unit test private methods?

I'm building a class library that will have some public & private methods. I want to be able to unit test the private methods (mostly while developing, but also it could be useful for future refactoring).

What is the correct way to do this?


Source: (StackOverflow)

Advertisements

NUnit vs. MbUnit vs. MSTest vs. xUnit.net [closed]

There are quite a lot of unittesting frameworks out there for .NET. I found this little feature comparison: http://xunit.github.io/docs/comparisons.html

Now I am to choose the best one for us. But how? Does it matter? Which one is most future proof and has a decent momentum behind it? Should I care about the features? While xUnit seems to be most modern and specifically designed for .NET, NUnit again seems to be the one that is widely accepted. MSTest again is already integrated into Visual Studio ...


Source: (StackOverflow)

differences between 2 JUnit Assert classes

The JUnit framework contains 2 Assert classes (in different packages, obviously) and the methods on each appear to be very similar. Can anybody explain why this is?

The classes I'm referring to are: junit.framework.Assert and org.junit.Assert.


Source: (StackOverflow)

How should I unit test threaded code?

I have thus far avoided the nightmare that is testing multi-threaded code since it just seems like too much of a minefield. I'd like to ask how people have gone about testing code that relies on threads for successful execution, or just how people have gone about testing those kinds of issues that only show up when two threads interact in a given manner?

This seems like a really key problem for programmers today, it would be useful to pool our knowledge on this one imho.


Source: (StackOverflow)

How do you test that a Python function throws an exception?

How does one write a unittest that fails only if a function doesn't throw an expected exception?


Source: (StackOverflow)

Writing unit tests in Python: How do I start? [closed]

I completed my first proper project in Python and now my task is to write tests for it.

Since this is the first time I did a project, this is the first time I would be writing tests for it.

The question is, how do I start? I have absolutely no idea. Can anyone point me to some documentation/ tutorial/ link/ book that I can use to start with writing tests (and I guess unit testing in particular)

Any advice will be welcomed on this topic.


Source: (StackOverflow)

What is a reasonable code coverage % for unit tests (and why)?

If you were to mandate a minimum percentage code-coverage for unit tests, perhaps even as a requirement for committing to a repository, what would it be?

Please explain how you arrived at your answer (since if all you did was pick a number, then I could have done that all by myself ;)


Source: (StackOverflow)

Run a single test method with maven

I know you can run all the tests in a certain class using:

mvn test -Dtest=classname

But I want to run an individual method and -Dtest=classname.methodname doesn't seem to work.


Source: (StackOverflow)

Unit tests vs Functional tests

What is the difference between unit tests and functional tests? Can a unit test also test a function?


Source: (StackOverflow)

NUnit vs Visual Studio 2008's Test Projects for Unit Testing? [closed]

I am going to be starting up a new project at work and want to get into unit testing. We will be using VS 2008, C#, and the ASP.NET MVC stuff. I am looking at using either NUnit or the built in test projects that VS2008 has, but I am open to researching other suggestions. Is one system better than the other or perhaps easier to use/understand than the other? I am looking to get this project set up as kind of the "best practice" for our development efforts going forward.

Thanks for any help and suggestions!!


Source: (StackOverflow)

Where do the Python unit tests go?

If you're writing a library, or an app, where do the unit test files go?

It's nice to separate the test files from the main app code, but it's awkward to put them into a "tests" subdirectory inside of the app root directory, because it makes it harder to import the modules that you'll be testing.

Is there a best practice here?


Source: (StackOverflow)

Conditionally ignoring tests in JUnit 4

OK, so the @Ignore annotation is good for marking that a test case shouldn't be run.

However, sometimes I want to ignore a test based on runtime information. An example might be if I have a concurrency test that needs to be run on a machine with a certain number of cores. If this test were run on a uniprocessor machine, I don't think it would be correct to just pass the test (since it hasn't been run), and it certainly wouldn't be right to fail the test and break the build.

So I want to be able to ignore tests at runtime, as this seems like the right outcome (since the test framework will allow the build to pass but record that the tests weren't run). I'm fairly sure that the annotation won't give me this flexibility, and suspect that I'll need to manually create the test suite for the class in question. However, the documentation doesn't mention anything about this and looking through the API it's also not clear how this would be done programmatically (i.e. how do I programatically create an instance of Test or similar that is equivalent to that created by the @Ignore annotation?).

If anyone has done something similar in the past, or has a bright idea of how else I could go about this, I'd be happy to hear about it.


Source: (StackOverflow)

What's the best strategy for unit-testing database-driven applications?

I work with a lot of web applications that are driven by databases of varying complexity on the backend. Typically, there's an ORM layer separate from the business and presentation logic. This makes unit-testing the business logic fairly straightforward; things can be implemented in discrete modules and any data needed for the test can be faked through object mocking.

But testing the ORM and database itself has always been fraught with problems and compromises.

Over the years, I have tried a few strategies, none of which completely satisfied me.

  • Load a test database with known data. Run tests against the ORM and confirm that the right data comes back. The disadvantage here is that your test DB has to keep up with any schema changes in the application database, and might get out of sync. It also relies on artificial data, and may not expose bugs that occur due to stupid user input. Finally, if the test database is small, it won't reveal inefficiencies like a missing index. (OK, that last one isn't really what unit testing should be used for, but it doesn't hurt.)

  • Load a copy of the production database and test against that. The problem here is that you may have no idea what's in the production DB at any given time; your tests may need to be rewritten if data changes over time.

Some people have pointed out that both of these strategies rely on specific data, and a unit test should test only functionality. To that end, I've seen suggested:

  • Use a mock database server, and check only that the ORM is sending the correct queries in response to a given method call.

What strategies have you used for testing database-driven applications, if any? What has worked the best for you?


Source: (StackOverflow)

Why does visual studio 2012 not find my tests?

I have some tests that use the built in Microsoft.VisualStudio.TestTools.UnitTesting, but can not get them to run.

I am using visual studio 2012 ultimate.

I have a solution of two projects; One has tests, using Microsoft.VisualStudio.TestTools.UnitTesting, [TestClass] before the class, [TestMethod] before the test methods and reference Microsoft.VisualStudio.QualityTools.UnitTestFramework (version 10.0.0.0, runtime version v2.0.50727). I have tried dot-net framework 3.5, 4 and 4.5 others give a re-targeting error.

I have tried to build the solution and project. Test explorer has the message `Build your solution to discover all available tests. Click "run all" to build, discover, and run all tests in your solution.

So the question is: How to I get visual studio to find the tests?


Have also tried to follow this: http://msdn.microsoft.com/en-US/library/ms379625%28v=VS.80%29.aspx but with no success: I get stuck in section getting started, when asked to right click and select create tests. There is no create tests.


I have this test(it compiles, but does not show up in test explorer):

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace tests {
    [TestClass]
    public class SimpleTest {
        [TestMethod]
        public void Test() {
            Assert.AreEqual("a","a", "same");
        }
    }
}

I have now discovered (see deleted answer below) that it is because it is on a shared drive, but I don't as yet know how to get around it. (something about the security setting maybe).


Source: (StackOverflow)