Tdd - creating tests for third-party code

How to create unit tests if the method or procedure I am testing depends on a part of the third-party code? Let's say I have a method that uses classes from a third-party source that requires an installation that can only be performed in a functional test. How do I approach this? In most cases, a third-party addiction cannot be taunted, but my method really should use it.

Also, should I avoid my unit tests or even my functional tests using real data? For example, should my tests never connect to the database APIs to temporarily add data, work and test it, and then delete it later?

+4
source share
3 answers

unit tests

you have to check everything. but not all with unit tests. unit tests are environment independent - databases, internet connections, etc. Best practices for working with unreliable / unstable third-party tools are to create a level of anti-corruption protection between your code and third-party code. therefore, edit your code to make your business logic as independent as possible. and a business logic block test.

, , " ". ( ), , . ,

- ( ), , , , , .

-

(db, network etc), . - -, , . SQL- .

, ( sql). , , , /. , SQL- db-in-memory db (oracle, postgres ..). , , . , .

+6

:

? , , , .

, API, .

.net , , .

, - :

public interface IMyDependency
{
    public void SomeMethod();

    public int CaclateSomething();

    //add other methods that you will call in the 3rd party dependency
}

, , , :

public class MyDependencyWrapper : IMyDependency
{
    private My3rdPartyDepency actualDependency;

    public MyDependencyWrapper(My3rdPartyDepency actualDependency)
    {
         this.actualDependency=actualDependency;
    }

    public void SomeMethod()
    {
        actualDependency.SomeMethod()l
    }

    public int CaclateSomething(string aParam)
    {
        return actualDependency.CalculateSomething(aParam);
    }

    //add other methods that you will call in the 3rd party dependency
}

, .

+4

, , . , .

, . , , , - , . mocks unit test .

mocks . unit test .

+3

All Articles