首页 \ 问答 \ TDD与单元测试[关闭](TDD vs. Unit testing [closed])

TDD与单元测试[关闭](TDD vs. Unit testing [closed])

我公司是对我们的代码进行单元测试的新手。 我一直在阅读有关TDD和单元测试的一段时间,并且相信它们的价值。 我试图说服我们的团队,TDD值得努力学习和改变我们的思维方式,我们如何编程,但它是一场斗争。 这带给我我的问题。

TDD社区中有很多人很关心写测试,然后是代码(和我在一起),但是对于正在与TDD挣扎的团队来说,折中还带来额外的好处?

一旦编写代码(可能作为检查代码的要求),我可能会成功地让团队编写单元测试,而我的假设是在编写单元测试时仍然有价值。

将挣扎的团队带入TDD的最佳方式是什么? 而不是这样,即使在代码写入之后它仍然值得编写单元测试?

编辑

我从中删除的是在编码过程中的某个地方开始单元测试是很重要的。 对于那些接手这个概念的团队来说,开始更多地采用TDD和测试。 感谢大家的意见。

跟进

我们最近开始了一个新的小项目,一小部分团队使用TDD,其余的在代码之后写了单元测试。 在我们封装了项目的编码部分之后,代码之后的这些写入单元测试惊讶地看到TDD编码器已经完成,并且使用了更为固定的代码。 这是赢得怀疑者的好办法。 我们仍然有很大的成长前进,但意志的战斗似乎已经结束。 感谢大家提供建议!


My company is fairly new to unit testing our code. I've been reading about TDD and unit testing for some time and am convinced of their value. I've attempted to convince our team that TDD is worth the effort of learning and changing our mindsets on how we program but it is a struggle. Which brings me to my question(s).

There are many in the TDD community who are very religious about writing the test and then the code (and I'm with them), but for a team that is struggling with TDD does a compromise still bring added benefits?

I can probably succeed in getting the team to write unit tests once the code is written (perhaps as a requirement for checking in code) and my assumption is that there is still value in writing those unit tests.

What's the best way to bring a struggling team into TDD? And failing that is it still worth writing unit tests even if it is after the code is written?

EDIT

What I've taken away from this is that it is important for us to start unit testing, somewhere in the coding process. For those in the team who pickup the concept, start to move more towards TDD and testing first. Thanks for everyone's input.

FOLLOW UP

We recently started a new small project and a small portion of the team used TDD, the rest wrote unit tests after the code. After we wrapped up the coding portion of the project, those writing unit tests after the code were surprised to see the TDD coders already done and with more solid code. It was a good way to win over the skeptics. We still have a lot of growing pains ahead, but the battle of wills appears to be over. Thanks for everyone who offered advice!


原文:https://stackoverflow.com/questions/1742323
更新时间:2022-05-23 06:05

最满意答案

根据MSDN MetadataToken在一个模块中是独一无二的 - 没有任何说明它保证任何订单。

即使它的行为方式是您想要的,那将是实现特定的,可以随时更改,恕不另行通知。

看到这个旧的MSDN博客条目 。

我强烈建议您远离任何依赖此类实施细节的信息 - 请参阅Marc Gravell的此回答 。

如果你在编译时需要一些东西,你可以看一下Roslyn (尽管它处于一个很早的阶段)。


On .net 4.5 (and even .net 4.0 in vs2012) you can do much better with reflection using clever trick with [CallerLineNumber] attribute, letting compiler insert order into your properties for you:

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class OrderAttribute : Attribute
{
    private readonly int order_;
    public OrderAttribute([CallerLineNumber]int order = 0)
    {
        order_ = order;
    }

    public int Order { get { return order_; } }
}


public class Test
{
    //This sets order_ field to current line number
    [Order]
    public int Property2 { get; set; }

    //This sets order_ field to current line number
    [Order]
    public int Property1 { get; set; }
}

And then use reflection:

var properties = from property in typeof(Test).GetProperties()
                 where Attribute.IsDefined(property, typeof(OrderAttribute))
                 orderby ((OrderAttribute)property
                           .GetCustomAttributes(typeof(OrderAttribute), false)
                           .Single()).Order
                 select property;

foreach (var property in properties)
{
   //
}

If you have to deal with partial classes, you can additionaly sort the properties using [CallerFilePath].

相关问答

更多
  • 这样的事情应该有效: var value = (string)GetType().GetProperty("SomeProperty").GetValue(this, null); Something like this should work: var value = (string)GetType().GetProperty("SomeProperty").GetValue(this, null);
  • 根据MSDN MetadataToken在一个模块中是独一无二的 - 没有任何说明它保证任何订单。 即使它的行为方式是您想要的,那将是实现特定的,可以随时更改,恕不另行通知。 看到这个旧的MSDN博客条目 。 我强烈建议您远离任何依赖此类实施细节的信息 - 请参阅Marc Gravell的此回答 。 如果你在编译时需要一些东西,你可以看一下Roslyn (尽管它处于一个很早的阶段)。 On .net 4.5 (and even .net 4.0 in vs2012) you can do much bett ...
  • 在stylelint中,是否可以为规则声明-block-properties-order嵌套数组? 不,这是不可能的。 你有两个选择: 编写一个提供这种灵活性的插件 。 要求边距必须在填充之前,或者反之亦然,在CSS代码中。 我建议将后一个选项作为更具体的属性顺序,团队成员更容易知道在哪里查看是否在声明块中使用了属性。 In stylelint is it possible to nest arrays for the rule declaration-block-properties-order? No, ...
  • 我建议使用BindingSource 。 这样,网格中的更改值将自动在列表中更改: BindingSource bs = new BindingSource(); bs.DataSource = yourList; dataGridView1.DataSource = bs; 这将解决您想要更新网格中手动更改的值的情况。 I would suggest to use BindingSource. This way a changed value in the Grid will automaticall ...
  • 扩展函数和属性不会添加到现有类中。 Kotlin编译器允许您引用它们,因为它们是类的一部分。 如果你在Kotlin中写一个名为'Funs.kt'的Kotlin文件中的函数'setPosition': //in file Funs.kt fun View.setPosition(value: Int) { //smth } 编译器将使其成为FunsKt.class,其中包含静态方法'setPosition'。 public final class FunsKt { public stati ...
  • 语言规范只是说修饰符必须在类型之前 ,因此int最后。 修饰符包括类型参数,注释,访问修饰符(private,protected,public), static , final , synchronized , strictfp , volatile , transient以及它们(来自“允许编译器”)的顺序。 前几天我做了一个谷歌搜索和static final比final static更频繁,所以这有助于订购他们:-) 我认为一般来说修饰符的顺序是最常见的: 注释 类型参数 访问修饰符 static fi ...
  • 您在FirstOrDefault运算符上缺少括号() 。 你也应该处理返回默认值的情况。 我建议在获取第一个或默认值之前选择Order值。 对于没有DisplayAttribute所有属性,这将返回0 : var prop = typeof(A) .GetProperties() .OrderBy(p => p.GetCustomAttributes(typeof(DisplayAttribute), true) .Cast
  • //Get all propertyes of class var allProperties = typeof(ReportObject).GetProperties(); //List that will contain all properties used in specific report List filteredProperties = new List(); fore ...
  • 事实证明,使用IsAssignableFrom检查您无法找到该接口是否是另一个接口的衍生物: Console.WriteLine(typeof(ICollection<>).IsAssignableFrom(typeof(ICollection))); Console.WriteLine(typeof(ICollection).IsAssignableFrom(typeof(ICollection<>))); 都会写错 ; 在这里没有什么帮助,这是我能得到的最佳解决方 ...
  • 代码结果不是谎言。 它基本上告诉我们这个课程不是你想象的那样。 您在项目的类路径中有多个不同版本的AEReportBean类,可能在不同的包中,并且导入了错误的类或在类加载中优先。 在Netbeans中进行类型/类搜索,以便在类路径中按给定名称查找所有类(我不做Netbeans,但在Eclispe中它是Ctrl + Shift + T,Netbeans等效可能是Alt + Shift + O) 。 更新 :另一个可能的原因是Netbeans没有在保存源文件时自动构建项目(IDE应该在构建期间创建/刷新.cl ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)