assert

Assert.AreEqual fails while it shouldn't

…衆ロ難τιáo~ 提交于 2019-11-28 07:38:07
I have a really weird behavior which I cannot explain. I have the following class: public class Project { public virtual int Id { get; set; } public virtual string Name { get; set; } } And a method which returns a Project object: public Project GetByName(string Name) { using (ISession session = NHibernateHelper.OpenSession()) { Project project = session.CreateCriteria(typeof(Project)) .Add(Restrictions.Eq("Name", Name)) .UniqueResult<Project>(); return project; } } I have added a Unit Test to test the GetByName method: [TestMethod] public void TestGetByName() { IProjectsRepository

How can I assert() without using abort()?

走远了吗. 提交于 2019-11-28 07:23:40
If I use assert() and the assertion fails then assert() will call abort() , ending the running program abruptly. I can't afford that in my production code. Is there a way to assert in runtime yet be able to catch failed assertions so I have the chance to handle them gracefully? Yes, as a matter of fact there is. You will need to write a custom assert function yourself, as C++'s assert() is exactly C's assert() , with the abort() "feature" bundled in. Fortunately, this is surprisingly straightforward. Assert.hh template <typename X, typename A> inline void Assert(A assertion) { if( !assertion )

Is it good practice to use assert in Java?

懵懂的女人 提交于 2019-11-28 07:18:13
I know that the keyword assert exists in java. However I don't remember seeing code that uses it. Probably I am using exceptions and logging in places where I could have used it. Is it a good practice to use the assert keyword in java? EDIT : I know that assertions in general is a good practice. my question is, to be more accurate, if in java the BKM of assertion is using the assert keyword rather than using exception, logging and other techniques. The main reason assertions are not used is because they are not enabled by default. Therefore if you have a condition that is important enough to

2.线性回归

☆樱花仙子☆ 提交于 2019-11-28 07:10:57
(一)简单线性回归 和之前介绍的KNN不同,KNN主要是解决分类问题,而线性回归顾名思义显然是用来解决回归问题的。线性回归具有如下特征: 解决回归问题 思想简单,实现容易 许多强大的非线性模型的基础,比如逻辑回归、多项式回归、svm等等 结果具有很好的可解释性 蕴含机器学习中的很多重要思想 图中是房屋的面积与价格之间的对应关系,不同的面积对应不同的价格,由此在二维平面中便形成了多个点。我们的目的就是要找到一条直线,最大程度上来拟合这些点。 但是在之前的KNN,分类问题中,横轴和纵轴都是样本的特征,而标签则是由这个点是红色还是蓝色决定的。 但是在线性回归中,由于是房产数据,我们必须要预测出一个具体的数值,而不能像分类问题那样,用简单的颜色来代表类别。而这些数据显然是在一个连续的样本空间中,因此需要一个坐标轴来表示。也正因为如此,在二维平面中只能有一个特征,要是多个特征,我们就要更高的维度上进行观察了。 如果样本的特征只有一个,我们称之为简单线性回归 我们的目的是要找到一个直线来尽可能多的拟合这些点,而在二维平面上显然是y = ax + b,那么每一个样本x,都会有一个真实值y和用拟合曲线预测出来的预测值ŷ,因此我们的真实值和预测值就会有一个差距 既然有真实值和预测值,那么评价一个直线的拟合程度,就看所有样本的真实值和预测值之差。如果只是简单的相减,那么两者之差可能有正有负,会抵消掉

How does Assert.AreEqual determine equality between two generic IEnumerables?

◇◆丶佛笑我妖孽 提交于 2019-11-28 05:45:26
I have a unit test to check whether a method returns the correct IEnumerable . The method builds the enumerable using yield return . The class that it is an enumerable of is below: enum TokenType { NUMBER, COMMAND, ARITHMETIC, } internal class Token { public TokenType type { get; set; } public string text { get; set; } public static bool operator == (Token lh, Token rh) { return (lh.type == rh.type) && (lh.text == rh.text); } public static bool operator != (Token lh, Token rh) { return !(lh == rh); } public override int GetHashCode() { return text.GetHashCode() % type.GetHashCode(); } public

Assert keyword in Java

爱⌒轻易说出口 提交于 2019-11-28 03:15:43
Do you use the assert keyword or throw some validation runtime exception? What benefits does it give to you or why do you think it's not worth it to use? andersoj Assert will throw a runtime error ( AssertionError ) if its condition is false. Asserts give you a streamlined way of documenting, checking, and enforcing correctness criteria for your code. The benefits are a language-level hook for defining and manipulating these correctness conditions. To the extent that you wish to enable or disable them (there are arguments about whether or not this is a good idea) you can do so from the JVM

How to enable assert in CMake Release mode?

会有一股神秘感。 提交于 2019-11-28 02:49:09
问题 CMake is being used to compile some C++ files. There are assert calls in the code. These calls are disabled in Release mode of CMake. It defines NDEBUG in Release mode, I guess. If I'm interested in having assert in Release mode of CMake, how do I enable it? 回答1: 1 If you interested in assert functionality only in your own code then the simple one solution is to provide custom assert. For instance: #if (MY_DEBUG) # define MY_ASSERT(A) ... checks here ... #else # define MY_ASSERT(A) ... ignore

Assert.AreEqual does not use my .Equals overrides on an IEnumerable implementation

守給你的承諾、 提交于 2019-11-28 02:35:11
问题 I have a PagedModel class which implements IEnumerable to just return the ModelData, ignoring the paging data. I have also overridden Equals and GetHashCode to allow comparing two PagedModel objects by their ModelData, PageNumber, and TotalPages, and PageSize. Here's the problem Dim p1 As New PagedModel() With { .PageNumber = 1, .PageSize = 10, .TotalPages = 10, .ModelData = GetModelData() } Dim p2 As New PagedModel() With { .PageNumber = 1, .PageSize = 10, .TotalPages = 10, .ModelData =

pytest + allure 生成测试报告

断了今生、忘了曾经 提交于 2019-11-28 00:56:08
pytest测试样例规则: 测试文件以test_开头(以_test结尾也可以) 测试类以Test开头,并且不能带有 init 方法 测试函数以test_开头 断言使用基本的assert即可 ubuntu 安装allure sudo apt-add-repository ppa:qameta/allure sudo apt-get update sudo apt-get install allure mac安装allure: brew install allure 源码安装参考地址: https://bintray.com/qameta/generic/allure2 安装pytest以及allure包: pip3 install pytest #不使用这个pip3 install pytest-allure-adaptor,用下面那个 pip3 install allure-pytest 创建一个用例 test_one.py: class TestClassOne(object): def test_one(self): x = "this" assert 't'in x def test_two(self): x = "hello" assert hasattr(x, 'check') class TestClassTwo(object): def test_one(self):

Should one override equals method for asserting the object equality in a unit test?

放肆的年华 提交于 2019-11-27 22:07:41
Let's say we are testing the result of a method by asserting the equality of all the properties of the result object with properties of an expected result object. Should we implement equals method and use Assert.AreEqual(expectedResult, actualResult)... But equals may mean something different in production code. Which is the best practice? Asserting the equality of the objects through overriden equals method or Asserting the equality of all the properties I for one use custom assertions. There are two main reasons: don't force test concerns into production. This means that the meaning of