How to test third-party third-party libraries?

Everyone says. Don't scoff at what you don’t have. . Instead, they recommend a shell for a third-party library.

But how can I check this shell?

I mean, if I write unit tests for it and mock these third-party interfaces to test my shell, nothing will change. If the library administrator changes his API, I will face the same problem - the tests for mocked lib will be fine, and the software will not work in a production environment. What is the advantage for reliability or testability here?

I know that there is an advantage in terms of code quality because I could exchange this library whenever I want, but this is not the subject of this question.

Thanks.

+5
source share
1 answer

There is a unit that tests your abstractions of third-party libraries to ensure that your code is working as intended, and then integration testing with third-party libraries is performed to ensure that the behavior of these integrals is consistent with the intended purpose. If there are sandboxes in these third-party libraries with which you can safely test, even better.

If, for example, your system under test depends on a third-party library, but you want a unit test without calling the actual library. You provide a mock implementation of a third-party interface, either manually or using a mocking structure.

public interface IExteralContract { List<string> DoSomething(params string[] args); } [TestMethod] public void SUT_Should_Be_True { //Arrange IExteralContract mock = new MockExternalContract(); var sut = new MyClass(mock); //Act var actual = sut.DoSomethingElse(); //Assert Assert.IsTrue(actual); } 

In this case, the system under test was one of your classes, depending on a third-party interface.

For your integration test, the system under test is the actual third-party shell to make sure that it behaves as intended.

 public class ConcreteExternalWrapper : IExteralContract { public List<string> DoSomething(params string[] args){ //Actual calling of 3rd party libraries } } [TestMethod] public void Third_Party_Service_Should_Return_Data { //Arrange IExteralContract sut = new ConcreteExternalWrapper (); int expected = 3; //Act var actual = sut.DoSomething("arg1", "arg2", "arg3"); //Assert Assert.IsNotNull(actual); Assert.AreEqual(expected, actual.Count); } 

Thus, if the library administrator changes their API, the behavior will cause the expected behavior to fail.

Hope this satisfies what you asked for.

+3
source

Source: https://habr.com/ru/post/1247899/


All Articles