简体   繁体   中英

Not getting how to write unit test function of my function

I have a function GetAssemblytree(Assembly assembly) which takes an assembly and returns all the assemblies referenced by given assembly in recursive manner.

I am not getting how should I write unit test function for this.

Any suggestion will be appreciated.

C# Assembly is an abstract class. You can simply extend it and have a dummy implementation of a concrete dummy class for test, and then everything naturally works.

You should not write a unit test for it, you should write several, and make each of them as simple as possible.

First, make sure you have a clear idea of what you want your function to do. Make each requirement as clear, simple and atomic as possible. Then define tests for each requirement.

You could define your test a little like this (just a rough sketch):

public void Should_return_null_if_assembly_does_not_reference_anything(){ 
    var result = YourFunctionToTest(yourEmptyDummyAssembly);
    ... 
    // Check result in a suitable way
}

public void Should_return_expected_assemblies(){
    var result = YourFunctionToTest(otherDummyAssembly);
    ... 
    // Check result in a suitable way
}

// etc...

The main rule of thumb to remember, is to keep things simple, and be really clear about what exactly it is you want to check with each test. Once you are clear on your goals, writing the tests themselves will not be that hard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM