简体   繁体   中英

Visual Studio C# Unit Tests

I'm trying to learn Unit Testing in C#. But I don't see how to relate the testmethods to the actual methods. In one guide it told me to right click the method name and click create unit test. I don't see that. Another told me to create a Unit Test Project in the solution and add my other project to the references. I still don't see how to reference a specific method or class to test in the testing project.

I see how it works, just not how to "target" the methods and classes I want to test. Since I can't instantiate them, I don't know what else to do. I try to instantiate because of protection level, but the class is public. I know I shouldn't change the namespace on the unit testing project's test class, but I don't know what else to do.

I keep reading tutorials and nothing seems to work the way they say.

Please give me a simple explanation and short example for C# unit testing.

For example 2 projects, one has namespace BankCS and class BankAccount, the other is UnitTest1 namespace and class BankAccountTest. In BankAccountTest when I try to instatiate bankaccount inside BankAccountTest I get BankCS.BankAccount.BankAccount() is inaccessible due to its protection level

From this line of code: var ba = new BankCS.BankACcount()

Tried changing the namespace to be the same as the project I am testing and it made no difference.

EDIT:

I got the errors fixed but am still getting System.Object is defined in an aseembly that is not referenced. You must add a reference to assembly System.Runtime....

Not sure how to do this, don't see it anywhere.

Also I understand unit tests I use them in Python a lot. But using them in Visual Studio and C# is a lot different. Which is why I would appreciated a short example. Also, why did the one tutorial say to right click the method and click create unit test, I've never seen that option just Run Tests and Debug Tests.

Seems my issue is, being able to instantiate what I want to test in my Unit Test Project, any tips for doing this? Any gotchas?

BankCS.BankAccount.BankAccount() is inaccessible due to its protection level

If the constructor isn't public then you can't access it from outside the class. This is true of all C# code (and pretty much object oriented code in general), nothing to do with unit tests.

If you want to create a new instance of an object, that object needs a public constructor:

public BankAccount()
{
    // constructor logic
}

Note that C# creates a public, parameterless constructor by default for all objects unless you provide a constructor. So if you're providing a non-public one:

private BankAccount() { /.../ }

then there won't be a default public one.

A unit test in its most simplistic sense is meant to test your code before your deploy it by use of automated testing capabilities rather than manually running through a test of your code strictly as a user. A good easily understandable overview of setting up a unit test for C# can be found here;

http://www.codeproject.com/Articles/391465/Creating-Unit-tests-for-your-csharp-code Also try looking through github for a unit test you can easily implement.

It sounds like the method or class you are trying to access is not public. Is it private, protected, or internal? If so, change the class and method to public. Another option is to use the InternalVisibleToAttribute so your tests can access classes/methods that are marked as internal.

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