使用 Nunit 测试构造函数
我是 Nunit 测试的新手,我想测试以下构造函数:
public class IngredientDAONHibernate : NutritionLibrary.DAO.IngredientDAO
{
private Configuration config;
private ISessionFactory factory;
public IngredientDAONHibernate()
{
try
{
config = new Configuration();
config.AddClass(typeof(NutritionLibrary.Entity.Ingredient));
config.AddClass(typeof(Entity.Nutrient));
config.AddClass(typeof(Entity.NutrientIngredient));
factory = config.BuildSessionFactory();
}
catch (Exception e)
{
// logger.Error("Exception Occured", e);
}
}
测试存根如下:
[TestMethod()]
public void IngredientDAONHibernateConstructorTest()
{
IngredientDAONHibernate target = new IngredientDAONHibernate();
}
有人可以帮助我提供一些关于如何开始的提示吗?谢谢!
I'm new to Nunit testing and I wanted to test the following constructor:
public class IngredientDAONHibernate : NutritionLibrary.DAO.IngredientDAO
{
private Configuration config;
private ISessionFactory factory;
public IngredientDAONHibernate()
{
try
{
config = new Configuration();
config.AddClass(typeof(NutritionLibrary.Entity.Ingredient));
config.AddClass(typeof(Entity.Nutrient));
config.AddClass(typeof(Entity.NutrientIngredient));
factory = config.BuildSessionFactory();
}
catch (Exception e)
{
// logger.Error("Exception Occured", e);
}
}
The test stub is as follows:
[TestMethod()]
public void IngredientDAONHibernateConstructorTest()
{
IngredientDAONHibernate target = new IngredientDAONHibernate();
}
Could someone help me with some tips as to how I can get started? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您不要捕获所有异常,但是,如果您想捕获并忽略某些异常,则只需执行这些异常即可,否则很难判断您是否有问题。
我倾向于不测试构造函数,除非它做了很多事情,并且在进行其他单元测试时是否出现问题会很明显,因为如果构造函数失败,您应该通过异常看到,并且通过事实所有测试都会失败。
如果您想测试此构造函数,请限制您的异常,并确保运行测试时没有异常。
I would suggest that you don't catch all the exceptions, but, if there are certain you want to catch and ignore then just do those, otherwise it is harder to tell if you have a problem.
I tend not to test the constructor, unless it is doing quite a bit, and it will be obvious if there is a problem when doing the other unit tests, as, if the constructor fails you should see by the exception, and by the fact that all the tests will be failing.
If you want to test this constructor, limit your exceptions and just ensure that there is no exception when you run the test.
让我把问题转回到你身上。你如何知道构造函数是否按预期执行?
通常构造函数是微不足道的..但这里似乎您有一些第三方库接口代码,您需要对它们有一定的信心。
如果您只想测试构造函数内是否没有引发异常...则提取记录器接口。现在,在模拟记录器中进行测试(假的也足够了),这应该可以帮助您感知是否记录了异常。
Let me turn the question back on to you.. How do you know if the constructor executed as intended ?
Normally constructors are trivial.. but here it seems that you have some third party lib interfacing code that you need some confidence with.
If you only want to test that there are no exceptions raised from within the constructor... then Extract a logger interface. Now in your test pass in a mock logger (a fake can also suffice), which should help you to sense if an exception was logged.
和其他人一样,我会警告不要捕获异常。文档应该告诉您可能出现哪些异常,并且您可以为每个异常编写特定的 Catch 子句(如果有意义的话)。
抛开这一点,您的测试应该验证您的对象是否处于您期望的状态。鉴于我们所看到的,您所能做的就是测试它是否不为空。如果可以强制异常,则可以测试记录器是否获取异常消息。如果有一个公共属性可以访问您的工厂,您可能需要测试它(不为空,也许是其他东西),同样使用配置字段。为此,如果您有权访问添加的类,则可以测试 not null 和 count==3。如果确切的对象很重要,您可以测试它们是否存在。决定对第三方东西的信任程度总是很棘手。一般来说,我会这样做,除非我有证据表明我不应该这样做。
当谈到测试方法时,尽可能测试每个参数。因此,对于字符串,测试 null 和空字符串加上相关(有效和无效)值。如果您需要字母数字,也可以测试特殊字符。如果数据有边界,则在边界两侧测试值加上典型值。例如,如果您期望一个介于 0 和 10 之间的 int,请测试 -1, 0, 1, 5, 9, 10, 11。编写只需要几秒钟,但当您尝试编写时,它可以在两年内节省您的时间修复一个错误。
Like everyone else, I would caution against catching Exception. The documentation should tell you what exceptions can be expected and you could write specific Catch clauses for each of them, if that made sense.
Leaving that aside, your tests should verify that your objects are in the state you expect. Given what we can see, all you can do is test that it's not null. If you can force an exception, you could test that the logger gets the exception message. If there was a public property that gave access to your factory, you would want to test that (not null, perhaps other things) and likewise with the config field. For that, if you have access to the added classes, you could test not null and count==3. If the exact objects are important, you could test that they are there. It's always tricky to decide how much to trust third-party stuff. Generally, I do unless I get evidence that I shouldn't.
When it comes to testing methods, test for each parameter whatever is possible. So, for a string, test null and emptystring plus relevant (valid and invalid) values. If you're expecting alphanumerics, test special chars as well. If data has boundaries test values on and either side of the boundary(ies) plus typical values. E.g. if you're expecting an int between 0 and 10, test -1, 0, 1, 5, 9, 10, 11. It only takes seconds to write but it can save you hours in two years when you're trying to fix a bug.