English 中文(简体)
如果我们从测试方法.net测试中受益,如何在守则中说明。
原标题:how to tell in code if we are running from a testmethod .net test

是否有办法知道何时要求使用标准?

bool MyMethod()
{
    if ( /* are we running a test? */ )
    {
        return true; // otherwise this will fail from the automated build script
    }
    else
    {
        // run the proper code
    } 
}

并且请让我不谈“这确实是坏的想法”:

最佳回答

你承认这可能是一种坏的想法,但你可能不知道这些替代办法。 http://en.wikipedia.org/wiki/Mock_object” rel=“noreferer”>mocking framework。 这是允许守则在测试和生产方面采取不同做法的既定做法。

关于主要问题:。

www.un.org/Depts/DGACM/index_spanish.htm 这取决于你所使用的单位测试框架。 我不了解联尼特(例如)的一个具体特征,即你正在接受测试。 其他框架(如管理测试)可能非常有利。 然而,很容易去做。

www.un.org/Depts/DGACM/index_spanish.htm 如果您对源代码重新具有约束力, 仅使用http://www.google.com/url?sa=t&source=web&cd=1&ved=0CAgQFjA&url=%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fyt3yck0x(VS.71).aspx&ei=PR6Se9yeQ78g08-i6C&gus=CNhE 有条件地界定某些变量的指令可以归责。

www.un.org/Depts/DGACM/index_spanish.htm 如果你重新对图书馆具有约束力, 我建议创建你自己的班子,你用来跟踪你是否在单位测试或实际编码中,并在《试验设计方法上设定这一班级,以表明你正在进行测试。 也可使用Environment.SetEnvironmentVariable,作为避免撰写特殊课程的一种方法。

问题回答

奥克,我把我的“座右铭”——s——一个真正坏的思想”评论排除在外。

您只能给出一个参数:<条码>bool是:RedelMode。

贵问题概述的方法: a 真正坏的想法。

如果你想要采用一种比其他方法不同的方法,则采取更清洁的办法,就是重新确定与这种方法的接口,并在测试时注入不同的执行。

例如:

// Interface whose implementation changes under testing
public interface IChangesUnderTest
{
  void DoesSomething();
}

// Inject this in production
public class ProductionCode : IChangesUnderTest
{
  void DoesSomething() { /* Does stuff */ }
}

// Inject this under test
public class TestCode : IChangesUnderTest
{
  void DoesSomething() { /* Does something else */ }
}

你们应该通过思考来检查用具的特性! 我将不提这个想法,因为你知道我所看到的情况;

我在此建议。

只是在某些有条件的假装中添加:

#if DEBUG

/* assume you are running in a test environment here */

#endif

这几乎不是坏的。 如果你确实需要的话,你可以查看<代码>#define -ing “TEST”,因为你可能要求你的测试代码在定期分解期间不执行。

如果你使用测试框架,并且知道testAssemblyName,你可以做到如下:

bool IsInUnitTest = AppDomain.CurrentDomain.GetAssemblies()
            .Any(a => a.FullName.StartsWith(testAssemblyName));

更具体地说,如果你使用联尼特,

bool IsRunningFromNUnit =
           AppDomain.CurrentDomain.GetAssemblies().Any(
                a=> a.FullName.ToLowerInvariant().StartsWith("nunit.framework"));

如果你使用MS 测试,那么将testAssemblyName作为相应的AssemblyName改为Microsoft.VisualStudio.QualityTools.Unitboramework





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...