lundi 29 janvier 2018

Different return to __cmp__ override in Python

I am writing some test cases to a __cmp_ Python override. My code looks like this:

def __cmp__(self, other):
    if self.name == other.name: 
        return 0
    return -1

When I test this way:

self.assertEqual(0, first_tail > first_tail)
self.assertEqual(0, first_tail < first_tail)

The test answers OK. But when I try:

self.assertEqual(0, first_tail == first_tail)

I get this error:

True != 0

Expected :0
Actual   :True

How to pass User Interaction in Cucumber CLI test

I have a delete script that deletes data and while doing so prompts to ask for proceed. I want to pass both my command and the user interaction "Y" in cucumber-cli

Scenario: Delete customer should delete all data Given the input When running the command """ abc delete """ Then the job should finish without throwing an error

I have tried echo Y | abc delete abc delete <<< Y printf Y | abc delete

But none seems to work inside scala-cucumber. It works only on terminal. What could be the problem?

That's my O/p

Press 'Y' to continue... Exception in thread "main" java.io.EOFException: Console has reached end of input at scala.io.StdIn$class.readChar(StdIn.scala:93) at scala.io.StdIn$.readChar(StdIn.scala:229)

Rails Minitest ActionView::Template::Error: undefined method for nil:NilClas

Been trying to debug this for two days and I'm just stuck. I have a view that I'm trying to test in rails and the view works perfectly when I test manually in the browser but I keep getting this error in my controller test:

ActionView::Template::Error: undefined method "name" for nil:NilClass Here is the full error message.

Here's the test (test/controllers/quotes_controller_test.rb):

  test "should get quotes index as browse" do
    get browse_path
    assert_response :success
  end

It's breaking where I render this partial (views/quotes/browse.html.erb):

  <% if @recent_quotes.any? %>
    <aside class="recent-quotes browse-quotes col-7">
      <%= render @recent_quotes %>
    </aside>
  <% end %>

The partial looks like this (views/quotes/_quote.html.erb):

<blockquote id="quote<%= quote.id %> blockquote">
  <small class="text-muted">
    <%= link_to quote.topic.name, artist_path(quote.topic) %>
  </small>
  <p class="mb-0"><%= link_to quote.content, quote_path(quote) %></p>

  <footer class="blockquote-footer">
      <cite title="Source">
        <%= link_to quote.speaker.name, quote.source %>
      </cite>
  </footer>
</blockquote>

And the controller action looks like this (controllers/quotes_controller.rb):

  def browse
    @artists = Artist.all
    @recent_quotes  = Quote.all.limit(7)
  end

Again, everything works great in the browser, but I can't get that simple test to pass. If I remove the partial it passes, so the route is working fine. I think that the test is just looking for the name method in the first call to quote.topic.name and not finding it.

But it's working in the browser, so I can't figure out what I'm doing wrong with this test.

Is it a good idea to use global variables for test data values?

First, a bit of context:

I have a class with a series of methods performing tests on a set of files.This class also has a setUpClass method where those files are created.

Over the lifetime of this class, names like fake.txt, foobar, etc. have been used, but they are all string literals used at three or four places and not actual variables.

I recently decided to change one of those string literals into something that better reflects its use (foobar to initial_value for example), but one of the test failed since I forgot to rename one of the occurrence of foobar.

To avoid facing this issue anymore, I decided to replace all of the string literals by global variables. For example:

TEST_FILE = 'fake.txt'

However, I found that this change make the test suite way less readable for a number of reasons:

  • Previously, it was easy to scan what was a file name and what was a file content because we alway used file extensions to indicate file names (i.e., foo.txt is a file, bar is what would be written in foo.txt). My change makes it harder to distinguish between the two at first glance.
  • Due to syntax highlighting of string literals, it was easy to scan where those fake values were used. Using variables makes them less distinguishable.
  • Variable names usually take more space (i.e., f.write('bar') over f.write(FAKE_FILE_CONTENT), with some more extreme examples in the actual code.

On the one hand (and considering the points I just made), the complexity of the file is low enough so we can use string literals without much more issue than forgetting one occurrence. On the other hand, it is unclear if this file is going to evolve at a later time, and I feel like it would be easier to fix the issue now (if we decide to fix it at all).

What are the pros and cons of the two approaches (beyond what I listed), and is there a preferred solution?

Jenkins Cobertura (with gcov) - what do the coverage statistics mean?

I'm currently writing unit tests for a Qt project. I wanted to use the statistics provided in Jenkins through the Cobertura plugin (underneath gcov is used to get the stats).

:~$ gcov -v gcov 5.4.0 20160609

:~$ gcc -v gcc version 5.4.0

However after I looked at the table (see below) I was really surprised to see the poor coverage especially of conditionals. For the first one (see Coverage Breakdown by File) I thought I was actually done, since the code has only three ifs(each with a single condition) and my tests covers all (checked this also through debugging just to make sure). So I am really confused what these numbers actually mean and how to interpret them in order to make my unit tests better.

enter image description here

I've even started thinking that some of the poor results might be due to the use of Qt since it's not exactly pure C++ and all "extras" (slots, signals, MOC files etc.) might be something that gcov can't handle properly.

Parallel or sequentially run tests on Go using the library testify?

There is a group of tests that each have a different option. I have a hunch to the fact that the tests run in parallel and it all depends on the order of the tests. Maybe someone worked with this library and will help. Thank you

Is there a way to not use @MockBean on Spring MVC tests?

I'm trying to test the Rest contract of my Rest Controller using @WebMvcTest, like:

@RunWith(SpringRunner.class)
@WebMvcTest(MyController.class)
class MyControllerTest {

The method that I'm testing use only MyFirstService, but inside the class there another two services (MySecondService and MyThirdService) used by two other methods.

The problem is the use of @MockBean. The test class seems like this:

@RunWith(SpringRunner.class)
@WebMvcTest(MyController.class)
class MyControllerTest { 

    @MockBean
    private MyFirstService s1; // internal method mocked for the test

    @MockBean
    private MySecondService s2; //never used

    @MockBean
    private MyThirdService s3; //never used

    @Test
    public void testMethod() [
        // mock s1
        // calls rest controller method
    }

}

I think that this solution is not elegant with these annotations... The declared s2 and s3 appears to not be used from who read the code. And every service that is injected in the MyController needs to be there with @MockBean.

So, I have two questions:

  1. Is there a way to not use @MockBean on Spring MVC tests?
  2. Is there a better way to do the same thing, even using @MockBean?