mstest

How can I stub IDBconnection

人走茶凉 提交于 2019-12-13 05:01:08
问题 I am writting Unit Test for my database connection. I have following class Public class A { public IDbConnection _dbConnection; public A() { _dbConnection = new SqlConnection(connectionStringName); } public int ExecuteNoneQuery(CommandDefination command) { return _dbConnection.Execute(command); } } I want to test this class, how I can test with Microsoft Stub/Shim I have written following code, but it is not working. [TestMethod] public void TestMethod1() { StubIDbConnection stubIDbConnection

Force developer to run unit test project before PR is Raised in Azure Devops

假装没事ソ 提交于 2019-12-13 03:28:36
问题 I am writing .Net 4.8 code and have MSTest tests in a single assembly dll. Using Azure Devops git as repository. The code in the branch should check-in only if all unit test PASS. Is it possible? I am also ok, if required, to use some script like powershell etc.. to help me enable this. 回答1: you can use branch policies to force a specific build to run and succeed before PR can be merged. You have to create a build that runs those tests and configure branch policies to run that build (consult

c# unittest change test outcome

雨燕双飞 提交于 2019-12-13 02:38:39
问题 for very specific reasons (explained below), I try to change the testoutcome after the execution of a unit test. I tried the following code: [TestClass] public class UnitTest1 { public TestContext TestContext { get; set; } [TestMethod] public void TestMethod1() { Assert.Fail(); } [TestCleanup] public void Cleanup() { TestContext.GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .First((fieldInfo) => fieldInfo.Name.ToLowerInvariant().Contains("outcome")) .SetValue

How does one debug an MSTest in VSCode?

喜夏-厌秋 提交于 2019-12-12 20:22:57
问题 In version 1.17.2 of VSCode (with the C# extenion installed) I have added an MSTest project to a solution folder via dotnet new mstest and added a reference to the the assembly being tested with dotnet add <project_path> . Given the two VSCode tasks below I can build and run the tests successfully; i.e. everything builds, the unit tests run and pass. { "version": "2.0.0", "tasks": [ { "taskName": "build", "command": "dotnet build src/tests/tests.csproj", "type": "shell", "group": { "kind":

Shared Unit Tests with MSTest

北慕城南 提交于 2019-12-12 18:35:27
问题 I have approximately 5-6 reports, they are structured the same, using Watin, I'm testing each one of these reports. I have a shared test, I call "ReportBaseTests".. public class ReportBaseTests { public string MenuName { get; set; } public ReportBaseTests(string name) { this.MenuName = name; } [TestMethod] public void Perform_Invalid_Date_Range() { } } but in each of my tests, I have... [TestClass] public class Report1Tests : ReportBaseTests { public Report1Tests() : base("Report 1") { } }

How do I get fake path for HttpContext.Current.Server.MapPath which is assigned to protected object inside method unit testing?

梦想的初衷 提交于 2019-12-12 18:18:55
问题 I am new to unit test, MSTest. I get NullReferenceException . How do I set HttpContext.Current.Server.MapPath for doing unit test? class Account { protected string accfilepath; public Account(){ accfilepath=HttpContext.Current.Server.MapPath("~/files/"); } } class Test { [TestMethod] public void TestMethod() { Account ac= new Account(); } } 回答1: HttpContext.Server.MapPath would require an underlying virtual directory provider which would not exist during the unit test. Abstract the path

MSTest project can't get localized string?

懵懂的女人 提交于 2019-12-12 10:54:30
问题 I ran into a strange problem. In my unit test, I want to check the localized strings. However, I can't seem to get it work. For example, I created two resources: Resource1.resx for English and Resource1.zh-CN.resx for Chinese. The unit test project can only get the (default?) English resource string. This is the code I'm using: ResourceManager actual = new ResourceManager(typeof(LocaleTest.Properties.Resource1)); string name0 = actual.GetString("Name", new CultureInfo("en-US")); string name1

Why PartCover report shows 0% when mstest runs successfully and all tests pass

倾然丶 夕夏残阳落幕 提交于 2019-12-12 10:54:17
问题 I'm trying to get code coverage with mstest tests. I'm using PartCover 2.2.0.36424. The problem is with real assemblies, I get 0% code coverage (Note: All tests pass). On demo test for demo source that I created, it worked fine (the report makes sense). I noticed that in log file: for demo files, after line "Assembly AAAAAA loaded (MyTestesAssemblyName)", there is line "Class NNNNNN loaded (MyTestesAssemblyName.MyClassname)"; and for the real files ther is no second line (for class) after the

(MSTest) Extending ExpectedExceptionBaseAttribute hides test failure explanation

天大地大妈咪最大 提交于 2019-12-12 10:12:49
问题 When running this test: [TestMethod] [ExpectedException(typeof(SpecialException))] public void Test1() { throw new NotImplementedException(); } Visual Studio tells me why it fails: Test method [...].Test1 threw exception System.NotImplementedException, but exception [...].SpecialException was expected. Exception message: System.NotImplementedException: The method or operation is not implemented. But when I try to extend ExpectedExceptionBaseAttribute (to expect for error code in

how often should the entire suite of a system's unit tests be run?

倖福魔咒の 提交于 2019-12-12 08:28:54
问题 Generally, I'm still very much a unit testing neophyte. BTW, you may also see this question on other forums like xUnit.net, et cetera, because it's an important question to me. I apoligize in advance for my cross posting; your opinions are very important to me and not everyone in this forum belongs to the other forums too. I was looking at a large decade old legacy system which has had over 700 unit tests written recently (700 is just a small beginning). The tests happen to be written in