我确信我在这里遗漏了一些简单的东西,但我无法弄清楚为什么我的 NUnit 对象比较测试仍然失败。
我有一个简单的对象:
public virtual int Id { get; private set; }
public virtual string Description { get; set; }
public virtual string Address { get; set; }
public virtual string Ports { get; set; }
public virtual string Password { get; set; }
public virtual ServerGroup ServerGroup { get; set; }
Run Code Online (Sandbox Code Playgroud)
我将该对象的一个实例保存到我的数据库中,然后使用 NHibernate 将其取出。我的 NUnit 单元测试将保存的对象与检索的对象进行比较并进行比较。我知道 AreSame() 会失败,因为它们不是对对象的相同引用,但我希望 AreEqual() 通过。
如果我调试测试,我可以看到两个对象在这些属性中似乎具有相同的值,我的测试仍然失败。有人能告诉我为什么吗?
谢谢!
我有以下类结构:
public class MyObj
{
public int Number;
}
public interface IService
{
int ProcessInt(MyObj obj);
}
public class Service : IService
{
public int ProcessInt(MyObj myObj)
{
return myObj.Number;
}
}
Run Code Online (Sandbox Code Playgroud)
然后是消费阶层
public class Class1
{
public void Run(IService s)
{
var obj = new MyObj {Number = 1};
Console.WriteLine(s.ProcessInt(obj));
}
}
Run Code Online (Sandbox Code Playgroud)
然后是单元测试
[TestFixture]
public class MyTest
{
private readonly Mock<IService> _service = new Mock<IService>(MockBehavior.Strict);
private readonly Class1 _sut = new Class1();
[SetUp]
public void SetUp()
{
var obj …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试为使用 mvvmcross 以 xamaring 编写的 android/ios 应用程序编写单元测试。我已经按照wiki 中的说明进行操作,当服务尝试以这种方式更改 ViewModel 时,它们确实运行良好:
var viewDispatcher = Mvx.Resolve<IMvxViewDispatcher>();
viewDispatcher?.ShowViewModel(
new MvxViewModelRequest(typeof(HomeViewModel), null, null, MvxRequestedBy.Unknown));
Run Code Online (Sandbox Code Playgroud)
测试在第一行失败Mvx.Resolve();。我认为这归结为在模拟 IoC 容器中注册接口:
this.mockDispatcher = new MockDispatcher();
this.Ioc.RegisterSingleton<IMvxViewDispatcher>(this.mockDispatcher);
this.Ioc.RegisterSingleton<IMvxMainThreadDispatcher(this.mockDispatcher);
Run Code Online (Sandbox Code Playgroud)
所以当以这种方式调用时,Mvx 无法解析。可以测试此代码还是有任何其他可能性可以从服务中更改 ViewModel?
我在 C# 中使用 Selenium 来创建我的自动化测试。现在我必须使用某种报告工具来保存测试用例是否通过,失败......等。
我见过很多工具,比如 Allure 和 Jenkins。但它们需要一个 XML 文件。我在使用 Visual 2013 运行测试时找不到它。这是为什么?
如果我遗漏了什么,如何详细获取这些 XML 文件?
有什么办法可以以最少的努力实现这一目标吗?
编辑: 如何使用 Nunit 控制台运行程序运行我的测试?我在哪里可以找到它?我下载了 nunit 的 .zip,但我找不到跑步者?
下面我粘贴了我的代码。我正在验证一项措施。我已经编写了读取 Linux 文件的代码。但如果我想在这里传递多个文件名,这可能吗?例如,我可以做一个循环,而不是只验证一个文件,这样它就可以一次性准备好多个文件。
一旦文件被读取并继续,我就返回actualItemData。在我的下一个方法中,我想对此进行调用,actualItemData以便数据发布在我的var actual
public string validateMeasurement
{
var processFilePath = **"/orabin/app/oracle/inputs/ff/ff/actuals/xx_ss_x.csv.ovr";**
var actualItemData = Common.LinuxCommandExecutor.
RunLinuxcommand("cat " + processFilePath);
**return actualItemData;**
}
public void validateInventoryMeasurementValue(string Data, string itemStatus)
{
var expected = '6677,6677_6677,3001,6';
**var actual = actualItemData);**
Assert.AreEqual(expected, actual);
}
Run Code Online (Sandbox Code Playgroud) 我完全不确定是什么导致了异常。
我有许多使用 NUnit TestCases 的测试,这些测试都运行良好,没有任何问题。然后我编写了一个额外的测试,除了几个变量值之外几乎与其他测试相同,现在由于 StackOverflowException,我的任何测试都不会运行。
[05/10/2018 10:10:00 Informational] ------ Run test started ------
[05/10/2018 10:10:00 Informational] NUnit Adapter 3.10.0.21: Test execution started
[05/10/2018 10:10:00 Informational] Running all tests in C:\test.dll
[05/10/2018 10:10:00 Informational] NUnit3TestExecutor converted 9 of 9 NUnit test cases
[05/10/2018 10:10:04 Error] The active test run was aborted. Reason: Process is terminated due to StackOverflowException.
[05/10/2018 10:10:04 Informational] ========== Run test finished: 0 run (0:00:04.661362) ==========
Run Code Online (Sandbox Code Playgroud)
另一个测试类中的测试将运行,但是即使我注释掉其他测试,该类中的任何测试也不会运行。
从下面的代码中可以看出,除了作为参数和测试用例传递的变量名称之外,大多数测试在结构上与其他测试相同。
[TestFixture]
class FluentAddressValidatorTest
{
private FluentAddressValidator validator;
[OneTimeSetUp]
public …Run Code Online (Sandbox Code Playgroud) 我想通过FluentAssertion语法检查方法的返回值。请考虑以下片段:
public interface IFoo
{
Task<int> DoSomething();
}
public class Bar
{
private readonly IFoo _foo;
private static int _someMagicNumber = 17;
public Bar(IFoo foo)
{
_foo = foo;
}
public async Task<int> DoSomethingSmart()
{
try
{
return await _foo.DoSomething();
}
catch
{
return _someMagicNumber;
}
}
}
[TestFixture]
public class BarTests
{
[Test]
public async Task ShouldCatchException()
{
// Arrange
var foo = Substitute.For<IFoo>();
foo.DoSomething().Throws(new Exception());
var bar = new Bar(foo);
Func<Task> result = () => bar.DoSomethingSmart(); …Run Code Online (Sandbox Code Playgroud) Visual Studio 2019 中的 MSTest v2 相对较新。该属性指示该方法应在每个测试之前TestInitialize运行。同样,指示该方法应在每次测试后运行。TestCleanup
[TestInitialize()]
public void Setup()
{
// This method will be called before each MSTest test method
}
[TestCleanup()]
public void Teardown()
{
// This method will be called after each MSTest test method has completed
}
Run Code Online (Sandbox Code Playgroud)
如果你的测试类有N个方法,上面的方法将运行N次。
有没有一种方法可以发出只运行一次的类似设置和拆卸的方法的信号?换句话说,对于所有 N 个测试的每次完整运行,每个方法将仅运行一次。
NUnit3 和 xUnit v2.4.0 是否有类似的机制?
我正在使用NUnit编写单元测试用例.在测试这个属性时,我得到了getter的覆盖,但不是setter.为什么?
private string name = null;
public string Name
{
get { return this.name; }
set
{
if (!String.IsNullOrEmpty(value) && value.StartsWith("@"))
{
name = value.Remove(0, 1);
}
else
{
name = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
单元测试:
[Test]
public void TestNameHaveValue()
{
classobject.Name = "@test";
//Assert
}
Run Code Online (Sandbox Code Playgroud) 我有这个代码
public class ClassToTest
{
private readonly IRepository repository;
public ClassToTest(DI GOES HERE){...}
public DoSomething()
{
Task.Run(async () => {
//some code
repository.ExecuteAsync();
}
}
}
public class Repository : IRepository
{
public Task ExecuteAsync()
{
using (var connection = new SqlConnection(DbConfiguration.DatabaseConnection))
{
return connection.ExecuteAsync(storedProcedure, parameters, commandType: CommandType.StoredProcedure, commandTimeout: Configuration.TransactionTimeout);
}
}
}
[Test]
public void TestMethod()
{
var repository = new Mock<IRepository>;
var classToTest = new ClassToTest();
classToTest.DoSomething();
repository.Veryfy(p => p.ExecuteAsync(), Times.Once());
}
Run Code Online (Sandbox Code Playgroud)
测试失败并显示此消息
模拟一次的预期调用,但是是0次:p => p.ExecuteAsync()
有谁知道为什么?
谢谢
nunit ×10
c# ×7
unit-testing ×4
allure ×1
asp.net ×1
async-await ×1
jenkins ×1
mocking ×1
moq ×1
mstest ×1
mvvmcross ×1
nhibernate ×1
nsubstitute ×1
selenium ×1
testing ×1
xamarin ×1
xunit ×1