How can I tell Moq to return a Task?

Your method doesn’t have any callbacks so there is no reason to use .CallBack(). You can simply return a Task with the desired values using .Returns() and Task.FromResult, e.g.: MyType someValue=…; mock.Setup(arg=>arg.DoSomethingAsync()) .Returns(Task.FromResult(someValue)); Update 2014-06-22 Moq 4.2 has two new extension methods to assist with this. mock.Setup(arg=>arg.DoSomethingAsync()) .ReturnsAsync(someValue); mock.Setup(arg=>arg.DoSomethingAsync()) .ThrowsAsync(new InvalidOperationException()); Update 2016-05-05 As Seth … Read more

How do I run all Python unit tests in a directory?

With Python 2.7 and higher you don’t have to write new code or use third-party tools to do this; recursive test execution via the command line is built-in. Put an __init__.py in your test directory and: python -m unittest discover <test_directory> # or python -m unittest discover -s <directory> -p ‘*_test.py’ You can read more … Read more

Mockito : how to verify method was called on an object created within a method?

Dependency Injection If you inject the Bar instance, or a factory that is used for creating the Bar instance (or one of the other 483 ways of doing this), you’d have the access necessary to do perform the test. Factory Example: Given a Foo class written like this: public class Foo { private BarFactory barFactory; … Read more

What is the difference between unit tests and functional tests?

Unit tests tell a developer that the code is doing things right; functional tests tell a developer that the code is doing the right things. You can read more at Unit Testing versus Functional Testing A well explained real-life analogy of unit testing and functional testing can be described as follows, Many times the development … Read more

Mocking static methods with Mockito

Use PowerMockito on top of Mockito. Example code: @RunWith(PowerMockRunner.class) @PrepareForTest(DriverManager.class) public class Mocker { @Test public void shouldVerifyParameters() throws Exception { //given PowerMockito.mockStatic(DriverManager.class); BDDMockito.given(DriverManager.getConnection(…)).willReturn(…); //when sut.execute(); // System Under Test (sut) //then PowerMockito.verifyStatic(); DriverManager.getConnection(…); } More information: Why doesn’t Mockito mock static methods?

How to unit test abstract classes: extend with stubs?

There are two ways in which abstract base classes are used. You are specializing your abstract object, but all clients will use the derived class through its base interface. You are using an abstract base class to factor out duplication within objects in your design, and clients use the concrete implementations through their own interfaces.! … Read more

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

If you’re brand new to using unittests, the simplest approach to learn is often the best. On that basis along I recommend using py.test rather than the default unittest module. Consider these two examples, which do the same thing: Example 1 (unittest): import unittest class LearningCase(unittest.TestCase): def test_starting_out(self): self.assertEqual(1, 1) def main(): unittest.main() if __name__ … Read more