Parameterized Test Example in .NET Core Using NUnit.

A lot of times when writing unit tests we end up with a lot of test methods that look the same and actually do the same thing. Also, there are special cases where we want to have high test coverage and in-depth test access for our crucial and very important core functionality methods.

For example, when creating a framework or a library, usually we want to write many tests and cover all possible aspects and outcomes, which may lead to a large amount of certain behavior test methods.

Very often, we end up with these test methods with the same logic and behavior but with different input and data values. We are going to create parameterized tests that will test the same method but with different values.

The Scenario

Let’s start with a simple method that calculates the total price of a product * quantity and then it applies the discount on the total price.

public static double calculate(double price,int quantity,double discount) 
{
    double totalPrice = price * quantity;
    double totalPriceWithDiscount = System.Math.Round(totalPrice - (totalPrice * discount/100),2);
    return totalPriceWithDiscount;
}

As simple as it looks, there is a lot of important other stuff to test here, like:

  • null’s
  • negative and zero inputs
  • exceptions/input validation handling
  • rounding

But we are not going to cover these here.

We are going to focus on the parameterized test and validating the mathematical correctness of the calculation method.

Without a parameterized test, we have this plain test.

[Test]
public void testCalculate() 
{
    Assert.AreEqual(100,MyClass.calculate(10,10,0));
}

This passes and, indeed, if the price of a product is 10, the quantity is 10, and we have zero discounts then the total price is 100.

The problem is that with this setup if we want to test different values and results we have to write a different test method for every different input.

The TestCase Attribute

We start by first converting the above test to a parameterized test using the TestCase attribute.

[TestCase(10,10,10,90)]
[TestCase(10,10,0,100)]
public void testCalculate(double price,int quantity,double discount,double expectedFinalAmount) 
{
    Assert.AreEqual(expectedFinalAmount,MyClass.calculate(price,quantity,discount)); 
}

Now, this method will run for every TestCaseannotation it has. A mapping will occur at runtime to the values we provided at the annotations and copied down to the method parameters. In our example, this test will run two times. We can pass reference types and value types.

Usually, the order of parameters goes by first providing the values and the last one is the expected result.

The TestCaseSource Attribute

For every different input, we have to add a TestCaseattribute at the top of the test method. To organize the code, and for reusability reasons, we are going to use the TestCaseSource attribute. We’re going to create a provider method and centralize the input data.

First, we create a provider method and then move and fill it with the data we want.

public static IEnumerable priceProvider() 
{
    yield return new TestCaseData(10,10,10,90);
    yield return new TestCaseData(10,10,0,100);
}

And also refactor the testCalculate method to use the priceProvider method.

[Test,TestCaseSource("priceProvider")]
public void testCalculate(double price,int quantity,double discount,double expectedFinalAmount) 
{
    Assert.AreEqual(expectedFinalAmount,MyClass.calculate(price,quantity,discount)); 
}

This is the same as having the TestCase attributes on top of the method.

We can also provide a different class for the provider methods to isolate and centralize the code in class/file level.

[Test,TestCaseSource(typeof(MyProviderClass),"priceProvider")]
public void testCalculate(double price,int quantity,double discount,double expectedFinalAmount) 
{
    Assert.AreEqual(expectedFinalAmount,MyClass.calculate(price,quantity,discount)); 
}

priceProvideris a static method inside MyProviderClass.

Extra Parameterization With the Help of the TestFixture Attribute

Let’s add one more step of parameterization with the help of TestFixture Attribute.Usually,TestFixture is a class attribute to mark a class that contains tests, on the other hand, one of the biggest features is that TestFixture can take constructor arguments. NUnit will create and test a separate instance for every input set.

Let’s assume that except for the final amount we test above, there is an extra amount applied depending on what category the product is, which could be category 1 or 2.

[TestFixture(typeof(int),typeof(double),1,5)]
[TestFixture(typeof(int),typeof(double),2,6.5)]
public class TestCharge
{
    T categoryType;
    X extraValue;
    public TestCharge(T t,X x) 
    {
        this.categoryType = t;
        this.extraValue = x;
    }
}

We know, in fact, that in category one the extra amount is 5 and in category two it’s 6.5.

We can now run all the tests again but also for every TestFixture we provided.

For example, we test the calculation also depending on the category.

[Test,TestCaseSource(typeof(MyProviderClass),"priceProvider")]
public void testCalculateCategory(double price,int quantity,double discount,double expectedFinalAmount) 
{
    Assert.AreEqual(expectedFinalAmount+(double)(object)this.extraValue,
                    MyClass.calculateCategory(price,quantity,discount,(int)(object)this.categoryType)); 
}

Final Words

Remember, what makes a good unit test is its simplicity, the ease of reading and writing, the reliability, not to be treated as an integration test, and it has to be fast.

The original and complete repository of code samples can be found here.

This article originally posted here

849 Replies to “Parameterized Test Example in .NET Core Using NUnit.”

  1. Pingback: custom keto
  2. Pingback: 토토사이트
  3. Pingback: live draw hk
  4. Pingback: Dekbedovertrekken
  5. Pingback: Cbd Capsules
  6. Pingback: prediksi hk
  7. Pingback: Buy CBD Flower UK
  8. Pingback: maricón
  9. Pingback: pr jobs
  10. Pingback: winnipeg used cars
  11. Pingback: 먹튀검증
  12. Pingback: Haier G7
  13. Pingback: 토토먹튀
  14. Pingback: news
  15. Pingback: Attitude Seeds
  16. Pingback: Paleo Diet
  17. Pingback: influensmi
  18. Pingback: poker99
  19. Pingback: data sgp
  20. Pingback: w88vn
  21. Pingback: filmaionline.net
  22. Pingback: guest
  23. Pingback: Pressure Washing
  24. Pingback: w88hello
  25. Pingback: uhynzeyl
  26. Pingback: 20mg cialis
  27. Pingback: viagra online
  28. Pingback: ventolin 50 mg
  29. Pingback: prednisolone 2mg
  30. Pingback: clomid late period
  31. Pingback: buy priligy reddit
  32. Pingback: goodrx diflucan
  33. Pingback: synthroid savings
  34. Pingback: neurontin tab
  35. Pingback: paxil forum
  36. Pingback: sildenafil usa
  37. Pingback: cymbalta overdose
  38. Pingback: vitamin d cymbalta
  39. Pingback: define cialis
  40. Pingback: drug finasteride
  41. Pingback: cialis otc status
  42. Pingback: cialis otc months
  43. Pingback: indian viagra
  44. Pingback: comprar viagra 100
  45. Pingback: buy cialis now
  46. Pingback: ed pills gnc
  47. Pingback: buy now
  48. Pingback: viagra song
  49. Pingback: buy ebay
  50. Pingback: 20mg low price
  51. Pingback: buy ebay
  52. Pingback: lovoo einloggen
  53. Pingback: 25 mg lisinopril
  54. Pingback: buy cialis ebay
  55. Pingback: cialis soft tablet
  56. Pingback: cialis 20mg usa
  57. Pingback: tadalafil 5 mg
  58. Pingback: fildena 50
  59. Pingback: viagra sample
  60. Pingback: fildena 50 mg
  61. Pingback: cialis effects
  62. Pingback: viagra for girls
  63. Pingback: stromectol 3mg
  64. Pingback: cialis canada
  65. Pingback: cialis ingredient
  66. Pingback: levitra 10 mg
  67. Pingback: lipitor 40 mg
  68. Pingback: losartan 100 mg
  69. Pingback: levitra 20mg
  70. Pingback: duloxetine hcl dr
  71. Pingback: cymbalta
  72. Pingback: bupropion 150 mg
  73. Pingback: generic celexa
  74. Pingback: bupropion 300 mg
  75. Pingback: flagyl antibiotic
  76. Pingback: supplements for ed
  77. Pingback: sildenafil online
  78. Pingback: cocaine and viagra
  79. Pingback: stromectol 0.5 mg
  80. Pingback: acyclovir
  81. Pingback: amoxicillin 500mg
  82. Pingback: what is cefdinir
  83. Pingback: cephalexin 500
  84. Pingback: zithromax cost uk
  85. Pingback: 5mg cialis cost
  86. Pingback: who makes viagra
  87. Pingback: daily viagra
  88. Pingback: cialis timeline
  89. Pingback: levitra medication
  90. Pingback: buy ivermectin
  91. Pingback: cialis from mexico
  92. Pingback: how to take cialis
  93. Pingback: otc viagra
  94. Pingback: viagra definition
  95. Pingback: cialis 2019
  96. Pingback: cialis daily pill
  97. Pingback: viagra cost
  98. Pingback: finasteride dosage
  99. Pingback: cat prednisolone
  100. Pingback: cialis 10mg india
  101. Pingback: viagra best price
  102. Pingback: sildenafil
  103. Pingback: plaquenil mexico
  104. Pingback: cialis usa
  105. Pingback: buy tadalafil
  106. Pingback: zithromax cost
  107. Pingback: regcialist.com
  108. Pingback: buy plaquenil uk
  109. Pingback: genic cialis
  110. Pingback: norvasc picture
  111. Pingback: ivermectin 6
  112. Pingback: sildenafil 100mg
  113. Pingback: effects of lipitor
  114. Pingback: cost of viagra
  115. Pingback: sildenafil generic
  116. Pingback: cialis 5 mg tablet
  117. Pingback: cialis daily
  118. Pingback: lilly cialis 5mg
  119. Pingback: cialis 20mg cheap
  120. Pingback: viagra cheap
  121. Pingback: 1
  122. Pingback: and duloxetine
  123. Pingback: viagrarover.com
  124. Pingback: canadian viagra
  125. Pingback: viagraherse.com
  126. Pingback: womens viagra
  127. Pingback: cialis 2.5 mg
  128. Pingback: doxycycline 100 mg
  129. Pingback: viagra otc mexico
  130. Pingback: ivermectin pills
  131. Pingback: cialis for sale
  132. Pingback: cialis 10mg
  133. Pingback: tonsillitis z pack
  134. Pingback: pour on ivermectin
  135. Pingback: salbutamol gsk
  136. Pingback: ed medications
  137. Pingback: cialis walmart
  138. Pingback: pills for erection
  139. Pingback: aperta
  140. Pingback: sildenafil 50 mg
  141. Pingback: best ed drugs
  142. Pingback: same day loan
  143. Pingback: cashino
  144. Pingback: cheap viagra usa
  145. Pingback: ivermectin 5
  146. Pingback: ivermectin 18mg
  147. Pingback: ivermectin 3mg
  148. Pingback: snorting viagra
  149. Pingback: prednisone 40 mg
  150. Pingback: chewable viagra
  151. Pingback: herb viagra
  152. Pingback: viagra pour homme
  153. Pingback: cheap viagra
  154. Pingback: natural viagra
  155. Pingback: blue pill viagra
  156. Pingback: whats viagra
  157. Pingback: viagra en ligne
  158. Pingback: goodrx sildenafil
  159. Pingback: viagra foods
  160. Pingback: valtrex prices
  161. Pingback: viagra cialis
  162. Pingback: women viagra
  163. Pingback: buy viagra 50 mg
  164. Pingback: viagra usa
  165. Pingback: plaquenil pill
  166. Pingback: female viagra
  167. Pingback: sildenafil prices
  168. Pingback: stromectol drug
  169. Pingback: fildena
  170. Pingback: ivermectin humans
  171. Pingback: male ed drugs
  172. Pingback: fake viagra pills
  173. Pingback: viagra pro
  174. Pingback: cialis tadalafil
  175. Pingback: cialis online
  176. Pingback: daily cialis pills
  177. Pingback: cialis price
  178. Pingback: vietnamese viagra
  179. Pingback: Biltricide
  180. Pingback: navarro pharmacy
  181. Pingback: ivermectin 3
  182. Pingback: tadalafil capsules
  183. Pingback: viagra erection
  184. Pingback: ivermectin 2%
  185. Pingback: albuterol 0.0
  186. Pingback: pharmacy tech
  187. Pingback: flccc ivermectin
  188. Pingback: i mask protocol
  189. Pingback: Glucophage
  190. Pingback: Voltaren
  191. Pingback: equimax ivermectin
  192. Pingback: Anonymous
  193. Pingback: flccc ivermectin
  194. Pingback: ivermectin
  195. Pingback: stromectol xr
  196. Pingback: Anonymous
  197. Pingback: ivermectin 12
  198. Pingback: ivermectin 250ml
  199. Pingback: stromectol cvs
  200. Pingback: cost of ivermectin
  201. Pingback: ivermectin ontario
  202. Pingback: flccc ivermectin
  203. Pingback: stromectol tablets
  204. Pingback: stromectol ireland
  205. Pingback: stromectol tablets
  206. Pingback: cialis pills
  207. Pingback: cialis kaufen
  208. Pingback: buy generic cialis
  209. Pingback: buy tadalafil
  210. Pingback: buy cialis canada
  211. Pingback: buy ivermectin nz
  212. Pingback: Online
  213. Pingback: ivermectin 5 mg
  214. Pingback: tadalafil 10mg
  215. Pingback: canadian tadalafil
  216. Pingback: buy cialis jelly
  217. Pingback: what is tadalafil
  218. Pingback: canada ed drugs
  219. Pingback: precio priligy
  220. Pingback: ivermectin uk
  221. Pingback: cialis tablets
  222. Pingback: cialis wirkung
  223. Pingback: cialis kopen
  224. Pingback: molnupiravir price
  225. Pingback: viagra porn
  226. Pingback: otc tadalafil
  227. Pingback: oral antiviral
  228. Pingback: 1acetylene
  229. Pingback: 1avoiding
  230. Pingback: tadalafil cost
  231. Pingback: cialis pour femme
  232. Pingback: tadalafil bnf
  233. Pingback: ivermectine kopen
  234. Pingback: viagra boots
  235. Pingback: cialis walmart
  236. Pingback: stromectol canada
  237. Pingback: viagra
  238. Pingback: borgata rewards
  239. Pingback: buy ivermectin uk
  240. Pingback: cheap cialis india
  241. Pingback: keflex antibiotico
  242. Pingback: mexico ivermectin
  243. Pingback: clomid tablet
  244. Pingback: ivermectina price
  245. Pingback: buy ivermectin nz
  246. Pingback: keflex ceclor
  247. Pingback: azithromycin stds
  248. Pingback: cefdinir emedicine
  249. Pingback: celebrex cancer
  250. Pingback: cialis replacement
  251. Pingback: ivermectina piojos
  252. Pingback: stromectol tablete
  253. Pingback: stromectol tablets
  254. Pingback: ivermectin lotion
  255. Pingback: ivermectin 0.1
  256. Pingback: singapore viagra
  257. Pingback: ivermectin 1mg
  258. Pingback: tadalafil reddit
  259. Pingback: ivermectin ebay
  260. Pingback: ivermectin canada
  261. Pingback: ivermectin 50 mg
  262. Pingback: ivermectin rx
  263. Pingback: cheapest ed pills
  264. Pingback: ed in men
  265. Pingback: use of ivermectin
  266. Pingback: mazhor4sezon
  267. Pingback: stromectol coupon
  268. Pingback: filmfilmfilmes
  269. Pingback: gRh9UPV
  270. Pingback: 9-05-2022
  271. Pingback: kinoteatrzarya.ru
  272. Pingback: TopGun2022
  273. Pingback: Xvideos
  274. Pingback: XVIDEOSCOM Videos
  275. Pingback: ivanesva
  276. Pingback: canadian drugs
  277. Pingback: Netflix
  278. Pingback: FILM
  279. Pingback: designchita.ru
  280. Pingback: YA-krasneyu
  281. Pingback: design-human.ru
  282. Pingback: designmsu.ru
  283. Pingback: vkl-design.ru
  284. Pingback: irida-design.ru
  285. Pingback: ivermectine
  286. Pingback: dengue ivermectin
  287. Pingback: projectio
  288. Pingback: psy online
  289. Pingback: Gz92uNNH
  290. Pingback: do-posle-psihologa
  291. Pingback: uels ukrain
  292. Pingback: bahis siteleri
  293. Pingback: DPTPtNqS
  294. Pingback: qQ8KZZE6
  295. Pingback: D6tuzANh
  296. Pingback: SHKALA TONOV
  297. Pingback: chelovek-iz-90-h
  298. Pingback: 3Hk12Bl
  299. Pingback: 3NOZC44
  300. Pingback: 01211
  301. Pingback: tor-lyubov-i-grom
  302. Pingback: film-tor-2022
  303. Pingback: hd-tor-2022
  304. Pingback: hdorg2.ru
  305. Pingback: JXNhGmmt
  306. Pingback: Psikholog
  307. Pingback: netstate.ru
  308. Pingback: Link
  309. Pingback: stromectol uk
  310. Pingback: psy
  311. Pingback: bit.ly
  312. Pingback: cleantalkorg2.ru
  313. Pingback: bucha killings
  314. Pingback: War in Ukraine
  315. Pingback: Ukraine
  316. Pingback: stromectol online
  317. Pingback: viagra
  318. Pingback: site
  319. Pingback: sildenafil citrate
  320. Pingback: stats
  321. Pingback: revatio
  322. Pingback: Ukraine-war
  323. Pingback: movies
  324. Pingback: gidonline
  325. Pingback: viagra generic
  326. Pingback: stromectol brand
  327. Pingback: web
  328. Pingback: film.8filmov.ru
  329. Pingback: video
  330. Pingback: cialis for bph
  331. Pingback: tadalafil 40 mg
  332. Pingback: filmgoda.ru
  333. Pingback: rodnoe-kino-ru
  334. Pingback: confeitofilm
  335. Pingback: stat.netstate.ru
  336. Pingback: sY5am
  337. Pingback: Dom drakona
  338. Pingback: JGXldbkj
  339. Pingback: aOuSjapt
  340. Pingback: ìûøëåíèå
  341. Pingback: psikholog moskva
  342. Pingback: A片
  343. Pingback: A片
  344. Pingback: porno}
  345. Pingback: Dim Drakona 2022
  346. Pingback: TwnE4zl6
  347. Pingback: psy 3CtwvjS
  348. Pingback: lalochesia
  349. Pingback: film onlinee
  350. Pingback: cialis otc
  351. Pingback: 3qAIwwN
  352. Pingback: video-2
  353. Pingback: sezons.store
  354. Pingback: psy-news.ru
  355. Pingback: 000-1
  356. Pingback: 3SoTS32
  357. Pingback: 3DGofO7
  358. Pingback: rftrip.ru
  359. Pingback: dolpsy.ru
  360. Pingback: kin0shki.ru
  361. Pingback: 3o9cpydyue4s8.ru
  362. Pingback: mb588.ru
  363. Pingback: kamagra medicine
  364. Pingback: newsukraine.ru
  365. Pingback: edu-design.ru
  366. Pingback: tftl.ru
  367. Pingback: brutv
  368. Pingback: site 2023
  369. Pingback: sitestats01
  370. Pingback: 1c789.ru
  371. Pingback: cttdu.ru
  372. Pingback: 1703
  373. Pingback: hdserial2023.ru
  374. Pingback: serialhd2023.ru
  375. Pingback: matchonline2022.ru
  376. Pingback: bit.ly/3OEzOZR
  377. Pingback: bit.ly/3gGFqGq
  378. Pingback: vidalista 2.5
  379. Pingback: bit.ly/3ARFdXA
  380. Pingback: bit.ly/3ig2UT5
  381. Pingback: bep5w0Df
  382. Pingback: www
  383. Pingback: icf
  384. Pingback: 24hours-news
  385. Pingback: rusnewsweek
  386. Pingback: uluro-ado

Comments are closed.