简体   繁体   English

Visual Studio C#单元测试

[英]Visual Studio C# Unit Tests

I'm trying to learn Unit Testing in C#. 我正在尝试用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. 请给我一个简单的解释和C#单元测试的简短示例。

For example 2 projects, one has namespace BankCS and class BankAccount, the other is UnitTest1 namespace and class BankAccountTest. 例如,2个项目,一个具有名称空间BankCS和类BankAccount,另一个是UnitTest1名称空间和类BankAccountTest。 In BankAccountTest when I try to instatiate bankaccount inside BankAccountTest I get BankCS.BankAccount.BankAccount() is inaccessible due to its protection level 在BankAccountTest中,当我尝试在BankAccountTest中实现BankCS.BankAccount.BankAccount() is inaccessible due to its protection level我得到BankCS.BankAccount.BankAccount() is inaccessible due to its protection level

From this line of code: var ba = new BankCS.BankACcount() 从这行代码: 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. 我修复了错误,但仍然得到System.Object是在未引用的aseembly中定义的。 You must add a reference to assembly System.Runtime.... 您必须添加对程序集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. 另外我理解我在Python中使用它们的单元测试很多。 But using them in Visual Studio and C# is a lot different. 但是在Visual Studio和C#中使用它们有很大不同。 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 由于其保护级别,BankCS.BankAccount.BankAccount()无法访问

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. 所有C#代码(以及一般的面向对象代码)都是如此,与单元测试无关。

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. 请注意, 除非您提供构造函数, 否则 C#默认为所有对象创建一个公共的无参数构造函数。 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; 可以在此处找到为C#设置单元测试的易于理解的概述;

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. http://www.codeproject.com/Articles/391465/Creating-Uni​​t-tests-for-your-csharp-code还可以尝试通过github查看您可以轻松实现的单元测试。

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. 如果是这样,请将类和方法更改为public。 Another option is to use the InternalVisibleToAttribute so your tests can access classes/methods that are marked as internal. 另一个选择是使用InternalVisibleToAttribute以便您的测试可以访问标记为内部的类/方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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