Tag: unit testing

VS 2010使用自定义计数器加载测试结果

我是Visual Studio 2010的负载测试(通常是测试)的新手,我正在处理几个问题。 我的问题是,有没有可能在负载测试结果上添加自定义测试变量? 我有以下UnitTest: [TestMethod] public void Test() { Stopwatch testTimer = new Stopwatch(); testTimer.Start(); httpClient.SendRequest(); testTimer.Stop(); double requestDelay = testTimer.Elapsed.TotalSeconds; } 许多LoadTests使用此UnitTest,我想将requestDelay变量添加到负载测试结果,因此我可以像所有其他负载测试计数器一样获得Min,Max和Avg值(例如测试响应时间)。 那可能吗?

我可以在Rhino-Mocks 3.6中使用AAA语法测试方法调用顺序吗?

如果Method1在Rhino-mocks 3.6中调用1st,然后调用After后面的Method2,然后使用AAA语法调用Method3,是否可以测试以下示例? // Assert var mock = MockRepository.GenerateMock(); // Act myObject.Service = mock; // How should I change this part to ensure that Rhino Mocks check the call order as well? mock.AssertWasCalled(m=>m.Method1()); mock.AssertWasCalled(m=>m.Method2()); mock.AssertWasCalled(m=>m.Method3());

MSTEST – 异步Testinitialize保证测试失败

只是想知道,如果有人这样想: 这是在TestInitialize中进行异步调用的错误设计,因为TestInitialize必须在任何TestMethod之前发生。 这可能是以任何方式进行异步TestInitialize的正确方法吗? private int val = 0; [TestInitialize] public async Task TestMehod1() { var result = await LongRunningMethod(); val = 10; } [TestMethod] public void TestMehod2() { Assert.AreEqual(10, val); } 任何想法?

C#unit testing – Thread.Sleep(x) – 如何模拟系统时钟

我必须测试一个间隔后做一定工作量的方法。 while (running) { … // Work … Thread.Sleep(Interval); } Interval作为参数传递给类,所以我可以传入0或1,但我对如何模拟系统时钟感兴趣,如果不是这样的话。 在我的测试中,我希望能够通过TimeSpan Interval简单地设置时间并让线程唤醒。 我从来没有编写过代码,这些代码之前会对执行的线程起作用,我确信有一些缺陷需要避免 – 请随意详细说明你使用的方法。 谢谢!

NSubstitute – 测试特定的linq表达式

我正在使用我正在开发的MVC 3应用程序中的存储库模式。 我的存储库界面如下所示: public interface IRepository where TEntity : IdEntity { void Add(TEntity entity); void Update(TEntity entity); void Remove(TEntity entity); TEntity GetById(int id); IList GetAll(); TEntity FindFirst(Expression<Func> criteria); IList Find(Expression<Func> criteria); } 在很多实例中,当我在服务类中编码方法时,我使用的是FindFirst和Find方法。 如您所见,它们都将linq表达式作为输入。 我想知道的是,NSubstitute是否允许您在代码中指定要测试的特定表达式。 所以,这是一个服务方法的例子,它说明了我提到的一个Repository方法的使用: public IList GetUnprocessedInvoices() { try { var invoices = _invoiceRepository.Find(i => !i.IsProcessed && i.IsConfirmed); var dtoInvoices = Mapper.Map<IList, IList>(invoices); […]

什么是SUT,它来自哪里?

我看到很多人都在谈论SUT这个术语,但不明白为什么他们会使用这个术语。 SUT是你想要测试的吗? 这个术语来自哪里,它意味着什么? 例如,在这个测试中,我的SUT是什么? [TestMethod] public void UsersAction_should_return_IndexAction() { const long id = 1; UsersViewModel viewModel = new UsersViewModel() { SelectedUsers = new long[] { 1, 2, 3, 4 } }; ActionResult result = _controller.Users(id, viewModel); result.AssertActionRedirect().ToAction(“Index”); }

访问方法’System.Web.Http.HttpConfiguration.DefaultFormatters()’失败

我的unit testing我的WEB API控制器有问题,我正在使用moq模拟我的存储库,进行设置和响应。 然后使用模拟存储库启动控制器。 问题是当我尝试从控制器执行调用时,我得到一个exception: 尝试通过方法’System.Web.Http.HttpConfiguration..ctor(System.Web.Http.HttpRouteCollection)’访问方法’System.Web.Http.HttpConfiguration.DefaultFormatters()’失败。 在System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection路由)处于EyeShield.Api.Tests.PersonsControllerTests.Get_Persons_ReturnsAllPersons()的System.Web.Http.HttpConfiguration..ctor()处 说实话,不知道这里可能出现什么问题。 有谁知道这里可能存在什么问题? 控制器: using System; using System.Net; using System.Net.Http; using EyeShield.Api.DtoMappers; using EyeShield.Api.Models; using EyeShield.Service; using System.Web.Http; namespace EyeShield.Api.Controllers { public class PersonsController : ApiController { private readonly IPersonService _personService; public PersonsController(IPersonService personService) { _personService = personService; } public HttpResponseMessage Get() { try { var persons = PersonMapper.ToDto(_personService.GetPersons()); […]

使用NUnit Console Runner在文件夹下运行所有​​测试

我正在尝试使用NUnit Runners 2.6.4在我的测试文件夹中运行所有测试程序集。 我当前的命令如下所示: /nologo /noshadow /framework:net-4.0 /xml:.\test\TestResults.xml .\test\*.Test.dll 不幸的是,Nunit只抛出一个System.ArgumentException:路径中的非法字符。 无论如何我能做到这一点吗?

我应该测试那些方法不会抛出exception吗?

我正在进行unit testing的第一步,并编写了(以及其他)这两种方法: [TestCase] public void InsertionSortedSet_AddValues_NoException() { var test = new InsertionSortedSet(); test.Add(5); test.Add(2); test.Add(7); test.Add(4); test.Add(9); } [TestCase] public void InsertionSortedSet_AddValues_CorrectCount() { var test = new InsertionSortedSet(); test.Add(5); test.Add(2); test.Add(7); test.Add(4); test.Add(9); Assert.IsTrue(test.Count == 5); } 真的需要NoException方法吗? 如果要抛出exception,它也将被抛出CorrectCount方法。 我倾向于将它保留为2个测试用例(可能将重复的代码重构为另一种方法),因为测试应该仅测试单个事物,但也许我的解释是错误的。

以编程方式断开网络连接

有没有办法以编程方式临时断开.NET 4.0中的网络连接? 我知道通过这样做我可以获得当前的网络连接状态…… System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() 但出于测试目的,我想测试我的应用程序丢失网络连接时的行为(没有物理拔出网络电缆)。 谢谢,克里斯。