nunit

.NET Core之单元测试(一):入门

烈酒焚心 提交于 2021-02-20 05:39:23
[toc] 什么是单元测试 单元测试 是对软件中的最小可测试单元进行检查和验证。对于单元测试,要保证测试粒度足够小,才能准确定位问题。通常而言,一个单元测试是用于判断某个特定条件(或者场景)下某个特定函数的行为。执行单元测试,是为了证明某段代码的行为确实和开发者所期望的一致。我们在编写测试代码时,应该要保证代码的自动执行,并非交互式的,保证测试用例的自动执行,不需要对测试结果进行人工验证。 单元测试除了协助本地开发,通常还放在持续集成中。每次有代码签入,单元测试都会被执行。因此单元测试要保证可重复执行,不能受外界环境影响。我们在编码设计时,可将外部依赖项通过DI容器注入。比如,我们有一个接口,依赖日志中间件,数据库等,我们就可以通过DI注入一个内存数据库和Mock对象来实现。 .NET Core中的测试框架 xUnit MSTest NUnit 一个最基础的单元测试 编写一段代码,实现一个功能:判断一个字符串是否时长文本(约定长度大于6即为长文本) public class UnitTestSample { public static bool IsStringLong(string val) { return val.Length > 6; } } 我们现在编写一个单元测试,测试长文本 本测试用例使用xUnit Install-Package xunit -Version 2.4

CAKE build and NUNIT3 generating empty results file

人走茶凉 提交于 2021-02-18 22:27:08
问题 I'm using cake build and trying to upload the cake unit test results to AppVeyor, but Cake/Nunit3 are generating empty results when I run locally and I assume that is what is causing my errors on AppVeyor. In the below block, NUnitResults.xml is generated but always empty. Task("UnitTest") .IsDependentOn("Build") .IsDependentOn("Setup") .Does(() => { var resultsFile = artifactsDirectory + "/NUnitResults.xml"; NUnit3("./StatusPageIo/StatusPageIo.UnitTests/bin/Release/StatusPageIo.UnitTests.dll

Using runsettings file when running NUnit tests via command line

早过忘川 提交于 2021-02-18 17:58:31
问题 I created a runsettings file which looks like this <?xml version="1.0" encoding="utf-8"?> <RunSettings> <TestRunParameters> <Parameter name ="environment" value="PROD" /> </TestRunParameters> </RunSettings> And then in my TestSetup portion (using LeanFT for UI tests) I specify that the target environment is contained under a paramater called environment string env= TestContext.Parameters["environment"]; This doesnt seem to work, and I am not getting any particular error messages. Is this the

Using runsettings file when running NUnit tests via command line

我怕爱的太早我们不能终老 提交于 2021-02-18 17:58:26
问题 I created a runsettings file which looks like this <?xml version="1.0" encoding="utf-8"?> <RunSettings> <TestRunParameters> <Parameter name ="environment" value="PROD" /> </TestRunParameters> </RunSettings> And then in my TestSetup portion (using LeanFT for UI tests) I specify that the target environment is contained under a paramater called environment string env= TestContext.Parameters["environment"]; This doesnt seem to work, and I am not getting any particular error messages. Is this the

Way to run NUnit tests within class in parallel, but not run with other class tests?

孤街浪徒 提交于 2021-02-18 17:57:07
问题 I'm using NUnit 3.8 in Visual Studio Professional 2017 . I've got [assembly: Parallelizable(ParallelScope.Fixtures)] designated in my AssemblyInfo.cs file. This makes the default behavior that tests within classes cannot run in parallel with other tests within that class, but they can run in parallel with tests of other classes. I'm wondering if it's possible to keep that default behavior but invert the logic on a class-by-class basis. I want to have tests within a specific class run in

NUnit: How to pass TestCaseData from a non-static method?

≯℡__Kan透↙ 提交于 2021-02-18 05:02:48
问题 my test fails because of : Message: The sourceName specified on a TestCaseSourceAttribute must refer to a static field, property or method. This is my Code: const double MAX_DELTA = 0.01; Qv_ges qv_ges_NE; double Sum_Qv_ges_R_FL; Qv_ges Qv_ges_Quer; [SetUp] public void init() { qv_ges_NE = Din1946.Calc_Qv_ges_NE(205.7d); Sum_Qv_ges_R_FL = 15d + 15d + 15d + 15d + 15d + 10d + 10d + 10d + 10d + 10d + 10d + 10d; Qv_ges_Quer = Din1946.Calc_Qv_ges_Quer(qv_ges_NE, Sum_Qv_ges_R_FL); } public

Jira Ticket reference in Nunit header

試著忘記壹切 提交于 2021-02-11 12:51:46
问题 We are creating Nunit tests which have corresponding Jira stories. We have been putting the ticket reference in the test name, but is there a convention or Nunit attribute that we should consider using instead which would be a useful or organised place for putting this reference instead? 回答1: Many folks use the [Description] attribute for this purpose. Another option is to use [TestOf] . While it's aimed at specifying the class or method that you are testing, you can use it for any other

Is there possibility to get Nunit “Property” attribute value if “Property” attribute is set for TestFixture level

泄露秘密 提交于 2021-02-07 18:30:11
问题 Here is a TestFixture class that I have: namespace TestApplication.Tests { [TestFixture] [Property("type","smoke")] public class LoginFixture { [Test] [Property("role","user")] public void can_login_with_valid_credentials() { Console.WriteLine("Test") } } } As you can see I set Nunit "Property" Attribute for Test and TestFixture levels. It is easy to get "Property" value from Test level: var test = TestContext.CurrentContext.Test.Properties["role"] But don't understand how to get "Property"

NUnit SetUpFixture attribute equivalent in xUnit?

拈花ヽ惹草 提交于 2021-02-07 03:19:16
问题 In nUnit, SetUpFixture allowed me to run some code before any tests. Is there anything like that when using xUnit? From nUnit documentation: This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace. 回答1: xUnit's comparison table shows that where you would use [TestFixtureSetUp] in NUnit, you make your test fixture class implement IUseFixture<T> . If [TestFixtureSetUp] isn't the attribute you're looking for,

NUnit SetUpFixture attribute equivalent in xUnit?

时光毁灭记忆、已成空白 提交于 2021-02-07 03:18:56
问题 In nUnit, SetUpFixture allowed me to run some code before any tests. Is there anything like that when using xUnit? From nUnit documentation: This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace. 回答1: xUnit's comparison table shows that where you would use [TestFixtureSetUp] in NUnit, you make your test fixture class implement IUseFixture<T> . If [TestFixtureSetUp] isn't the attribute you're looking for,