在 C# 中的测试运行持续时间中排除测试初始化时间 [英] Exclude test initialization time in a test run duration in C#
问题描述
我有几个延迟测试(基本上是单元测试)来测试特定的服务器 API,我必须准确地知道从请求到响应需要多长时间.我遇到的问题是,在调用测试之前,我必须做一些数据初始化(对于每次测试运行,所以如果我做一个运行 100 次的延迟测试,数据也必须初始化 100 次),这需要一些时间,但是我不想算了.
I have a couple of latency tests (basically unit tests) which test specific server APIs, and i have to know precisely how much time does it take from a request to response. the problem i have is that before calling a test, i must do some data initialization (for each test run, so if i do a latency test that runs 100 times, data must be initialized 100 times as well) which takes some time, but i don't want to count it.
初始化数据有两种方式:[MethodInitialization] 和 [ClassInitialization].
There are 2 ways to initialize data : [MethodInitialization] and [ClassInitialization].
MethodInitialization - 在每次运行之前初始化数据,但在测试完成后,其持续时间 = 测试初始化时间 + 测试运行
MethodInitialization - inits data before each run but when the test is finished, its duration = test initialization time + test run
ClassInitialization - 只初始化一次数据(这对我不起作用),但测试持续时间 = 测试运行
ClassInitialization - inits data only once (which doesnt work for me), but test duration = test run
我知道秒表,但使用起来会很痛苦,因为我也从 AppInsights 获取数据,并希望尽可能自动化,如果我能在简单运行后得到清晰的结果,那就是了很棒.
I know about stopwatch, but it will be very painful to use, as i also get data from AppInsights and would like to automate it as much as possible, and if i can have clear results after simple run, that would be just great.
有没有办法在每次测试运行之前初始化测试数据但排除其持续时间?
Is there a way to initialize a test data before each test run but exclude its duration ?
更新 1:我还尝试创建我的自定义属性,因为可以在其构造函数中执行某些操作,但是即使我运行测试 10 次,属性构造函数也只调用一次.
UPD 1: I also tried to create my Custom Attribute, as there is way to do some actions in its constructor, but even when i run the test 10 times, attribute constructor is called just once.
推荐答案
我猜你需要一个像 ANTS Performance Profiler 这样的专业工具.它将捕获您例程(类或方法)的每一步 - 您可以设置里程碑"并丢弃其他.
I guess you need a professional tool like ANTS Performance Profiler. It will catch every single step of your routine (Class or Method) - and you can set "milestones" and discard others.
但是,我正在考虑您上面提到的有关 METHOD 计时的内容...我认为这是正确的做法,因为您不能在例行程序中放弃该步骤.
But, I'm thinking about what you mention above about the METHOD timing... I think it's the correct way to do it, since you cannot discard that step in your routine.
如果我是您的客户,并且您的例程在加载数据期间消耗了大量时间,但运行速度非常快,那么我将了解整个时间来判断性能.我不会将您的例程分为两个模块:准备和执行.
If I'm your client and your routine consumes a lot of time during the load of data but runs really fast, I will have the idea about the entire time to my judgement about performance. I won't separate your routine in two modules: its preparing and its execution.
我希望我在某种程度上帮助了你.祝你好运.
I hope I had helped you some way. Good luck.
这篇关于在 C# 中的测试运行持续时间中排除测试初始化时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!