我正在研究一个实用的单元测试实验室.下面是我正在测试的应用程序(我正在测试所有方法和构造函数).我的所有测试都是完全接受的,即测试一个名为"isScalene()"的方法,检查三角形是否是一个斜角(所有边都是唯一的).
您会发现底部失败的测试方法.当我将"equalateral"改为True而"scalene"改为False时,它会通过.应用程序出了问题,但我无法弄清楚它是什么(可能在"uniqueSides()"中).
如果有人能帮助我,我将不胜感激!
public class Triangle {
double[] sides;
public Triangle(double a, double b, double c) {
if ((a <= 0) || (b <= 0) || (c <= 0)){
throw new ArgumentOutOfRangeException("Sidorna måste vara större än 0.");
}
sides = new double[] { a, b, c };
}
private int uniqueSides() {
return sides.Distinct<double>().Count();
}
public bool isScalene() {
if (uniqueSides() == 1){
return true;
}
else{
return false;
}
}
public bool isEquilateral() {
if (uniqueSides() == 3){
return …Run Code Online (Sandbox Code Playgroud) 对于检索实时股票价格的应用程序,我发现我的单元测试断言由于两个调用之间的价格波动而返回假阴性,这些调用填充了包含expected和actual测试值的变量.
虽然这是可以预期的,但我希望听到有关如何解决这个问题的不同方法.我最初的想法是允许波动幅度(检索到的股票价格之间差异约为2%)
这是向Yahoo发出Web请求以检索股票价格的代码.
public string makeWebRequest(string stockSymbol, string dataRequestID)
{
string request = webClient.DownloadString("http://finance.yahoo.com/d/quotes.csv?s=" + stockSymbol +
"&f=" + dataRequestID).Replace("\r\n", "").Replace("\"", "");
if (request.Equals("N/A") || request.Equals("0"))
return "0.00";
return request;
}
public string getPrice(string stockSymbol)
{
return makeWebRequest(stockSymbol, "l1");
}
Run Code Online (Sandbox Code Playgroud)
这是单元测试,它对股票价格进行"硬编码"(已知是成功的)Web请求并将结果分配给expected变量.然后,执行另一个调用,以便仅使用应用程序object.function检索价格,然后将其分配给actual变量.
呼叫之间的增量为300毫秒
[TestMethod]
public void getPrice()
{
string expected = request.DownloadString("http://finance.yahoo.com/d/quotes.csv?s=" + testSymbol + "&f=l1").Replace("\r\n", "").Replace("\"", "");
string actual = yahoo.getPrice(testSymbol);
Assert.AreEqual(expected, actual);
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?或者我应该学会忍受它?
我有这样的测试方法
[TestMethod]
public void TestMethod1()
{
var smthing= new doSmthing();
smthing.doSomefunction("Test Service","Test Operation");
// do something next
var 2ndsmthing = do2ndSmthing();
2ndsmthing.do2ndSomeThing("Test","Method")
}
Run Code Online (Sandbox Code Playgroud)
相信我这两个函数或调用需要在同一个测试方法下如何在调用第一个方法时如果出现问题就能阻止测试停止?即在调用doSmthing()时,我听说在测试方法中使用Try ... Catch块是一个坏主意.我怎么解决这个问题?
任何想法真的很感激.
我想在代码中从一个地方部署一个文件,我不想用相同参数的相同属性来装饰很多测试.
我用mstest.
我在库中有以下类,我必须编写UnitTests,并由我的开发团队给它编写单元测试.
internal sealed class Settings : AppSettingsBase
{
private static Settings defaultInstance = ((Settings)(Synchronized(new Settings())));
public static Settings Default { get { return defaultInstance; } }
}
Run Code Online (Sandbox Code Playgroud)
因为它的内部我无法在我的.Tests项目中访问它.当我查看stackoverflow时,建议是要求开发人员在AssemblyInfo.cs文件中放置一个属性[assembly:InternalsVisibleTo("MyTests")].这是我要求我的开发人员,但它需要一些时间.
有没有办法通过任何反射概念或任何其他概念来读取这些属性或变量.我在我的测试项目中使用Microsoft FAkes.
public static T ToObject<T>(this DataRow row) where T : new()
{
if (row == null) throw new ArgumentNullException("row");
// do something
}
public static IEnumerable<T> ToObject<T>(this DataTable table) where T : new()
{
if (table == null) throw new ArgumentNullException("table");
// do something
}
Run Code Online (Sandbox Code Playgroud)
以及各自的测试:
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void NullDataRow()
{
// Arrange
DataRow row = null;
// Act
row.ToObject<SomeData>();
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void NullDataTable()
{
// Arrange
DataTable table = null;
// Act
table.ToObject<SomeData>();
}
Run Code Online (Sandbox Code Playgroud)
该 …
我有一个非常简单的方法,它查询数据库并返回一个值.代码如下:
public List<int?> TravelTime()
{
List<int?> items = new List<int?>();
Induction induction = new Induction();
using (var dbContext = new MyEntites())
{
var query = dbContext.MyTable.Select(a => a.Travel_Time).Take(1);
foreach (var item in query)
{
induction.TravelTime = item;
items.Add(induction.TravelTime);
}
}
return items;// Value here is 8
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用以下代码对此方法进行单元测试:
[TestMethod]
public void Check_Travel_Time_Test()
{
//Arrange
InductionView vModel = new InductionView();
Induction induction = new Induction();
List<int?> actual = new List<int?>();
induction.TravelTime = 8;
actual.Add(induction.TravelTime);
//Act
var expected = vModel.TravelTime();
//Assert
Assert.AreEqual(expected, …Run Code Online (Sandbox Code Playgroud) 我正在为 Sql Server 2014 中的 db 代码开发单元测试框架。我的要求如下:
在做了一些研究之后,我想在工具 SSDT 和 MSTest 和 tSqlT 的帮助下,我有三个选择:
任何人都可以在 db 单元测试中提出更好的方法吗?
谢谢
我仍在学习在我的应用程序中使用 MSTest 和 Moq 进行自动化单元测试。我已经成功地模拟了代码并运行了它。它表明测试通过,但代码覆盖率为 0%。这是我下面的代码。需要更改什么才能使代码覆盖率变为 100%。
我知道这个问题之前曾被问过几次,但似乎没有任何帮助。所以有人可以告诉我我做错了什么。
非常感谢任何帮助。谢谢。
PS:我使用 Sonarcube 来了解代码覆盖率。
using Moq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
using System.Diagnostics.CodeAnalysis;
namespace MyNameSpace
{
[TestClass]
public class ApplicationTest
{
readonly Helper moqHelper = new Helper();
[TestMethod()]
public void GetDataFromDataBaseMoq()
{
Task<bool> returnValue;
Mock<Application> mockType = new Mock<Application>();
mockType.CallBase = true;
mockType.Setup(x => x.GetDataFromDataBase()).Returns(returnValue = moqHelper.GetDataFromDataBaseMoq());
if (returnValue.Result)
{
Assert.IsTrue(true);
}
else
{
Assert.Fail();
}
}
}
[ExcludeFromCodeCoverage]
class Helper
{
internal async Task<bool> GetDataFromDataBaseMoq()
{
bool returnValue = true;
return returnValue; …Run Code Online (Sandbox Code Playgroud) 在阅读和试验 Stack Overflow 中的可用帖子后,我问了这个问题。
我正在努力在 powershell 中触发 MSTEST
这是我的尝试(显然是在堆栈溢出中可用的帖子的帮助下)
$testDLL = "C:\Automation\Tests\My.Tests.dll"
$fs = New-Object -ComObject Scripting.FileSystemObject
$f = $fs.GetFile("C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe")
$vstestPath = $f.shortpath
$arguments = " " + $testDLL + ' /TestCaseFilter:"TestCategory=FunctionalTests"'
Write-Host $arguments
& $vstestPath $arguments
Run Code Online (Sandbox Code Playgroud)
控制台输出:
PS C:\Users\myuser\Desktop\Powershell scripts> .\exp5.ps1
C:\Automation\Tests\My.Tests.dll /TestCaseFilter:"TestCategory=FunctionalTests"
Microsoft (R) Test Execution Command Line Tool Version 15.9.1
Copyright (c) Microsoft Corporation. All rights reserved.
VSTEST~1.EXE : The test source file "C:\Automation\Tests\My.Tests.dll /TestCaseFilter:TestCategory=FunctionalTests" provided was not found.
At C:\Users\myuser\Desktop\Powershell scripts\exp5.ps1:7 char:1 …Run Code Online (Sandbox Code Playgroud) mstest ×10
c# ×8
unit-testing ×7
assert ×1
deployment ×1
generics ×1
moq ×1
powershell ×1
tfs ×1
tsqlt ×1