samedi 27 avril 2019

How can I implement JUnit testing into this program for a binary search?

How can I implement JUnit testing into this program for a binary search, i am quite overwhelmed by this task because I am really unfamiliar with JUnit and also I have all these packages and path that you see in code that I need to use.

I have already tried implementing Testing for just a normal Java class made by me but that did not work either, it would be great if someone could also explain me the syntax of some tests for my code.

package de.hska.iwi.ads.solution.search;

import java.security.acl.LastOwnerException;

import de.hska.iwi.ads.search.Search;

public class BinarySearch<E extends Comparable<E>> implements Search<E> {


@Override
public int search(E[] a, E key, int lower, int upper) {
    // TODO Auto-generated method stub

    this.lower = lower;
    this.upper = upper;

    if(upper > a.length) {
        throw new ArrayIndexOutOfBoundsException();
    }
    int ret = binarySearch(a, key, lower, upper);

    return returnValue;
}

int lower;
int upper;
int returnValue;



/**
 * 
 * @param a Array, der durchsucht werden soll.
 * @param key Element, nach dem gesucht wird.
 * @param lower untere Grenze des zu durchsuchenden Bereiches.
 * @param upper obere Grenze des zu durchsuchenden Bereiches.
 * @return index der Stelle wo daa Elemnt ist.
 */
private int binarySearch(E[] a, E key, int lower, int upper) {

    if (lower < upper) {

        int middle = (lower / 2) + (upper / 2);
        int tempInt = a[middle].compareTo(key);

        if (tempInt > 0) {

            return binarySearch(a, key, lower, middle - 1);
        }
        if (tempInt < 0) {
            return binarySearch(a, key, middle + 1, upper);
        }

        this.returnValue = middle;
        if (key.equals(a[middle]) && !key.equals(a[middle-1])) {
            return middle;
        } else {
            return binarySearch(a, key, lower, middle-1);
        }
    }

    if (key.equals(a[lower])) {
        this.returnValue = lower;

        int temp = checkForDuplicates(a, key, 0, upper-1);

        return returnValue;
    }

    int temp = key.compareTo(a[this.upper]);
    if(temp > 0) {
        this.returnValue = (this.upper + 1);
        return (this.upper + 1);
    }
    temp = key.compareTo(a[this.lower]);
    if(temp < 0) {
        this.returnValue = this.lower - 1;
        return (this.lower - 1);
    } else {
        this.returnValue = upper + 1;
    }


    return returnValue;

}

int lastIndex;

private int checkForDuplicates(E[] a, E key, int lower, int upper) {

    if (lower < upper) {

        int middle = (lower / 2) + (upper / 2);
        lastIndex = middle;
        int tempInt = a[middle].compareTo(key);

        if (tempInt < 0) {
            return checkForDuplicates(a, key, middle + 1, upper);
        }

        this.returnValue = middle;
        if (key.equals(a[lower])) {
            this.returnValue = lower;
            checkForDuplicates(a, key, 0, middle-1);
            return returnValue;
        }

        return -1;
    }

    if (key.equals(a[lower])) {
        this.returnValue = lower;
        return returnValue;
    } 

    return -1;
}


}

Django testt: AttributeError: 'WSGIRequest' object has no attribute 'user'

The test:
1) login with a user
2) favourite a post

class FormTest(TestCase):
    def setUp(self):

        self.user = ProjectUser.objects.create(username='testUser',
                                               email='testUser@user.com')
        self.user.set_password('testPassword')
        self.user.save()
        self.client = Client()
        self.factory = RequestFactory()


def sometest(self):

        self.client.login(username='testUser', password='testPassword')  
        user = auth.get_user(self.client)  
        assert user.is_authenticated 

        new_object_2 = SampleModel.objects.create(unique_id='123456', name='sample')

        request = self.factory.post(reverse('page1:favourite_post', kwargs={'fav_id': new_object_2.id}))
        response = favourite_post(request, new_object_2.id)
        print(response)

My url file is

path('favourite_post/<int:fav_id>', views.favourite_post, name='favourite_post'),

The views.py is

def favourite_post(request, fav_id):
    post = get_object_or_404(Post, id=fav_id)

    if request.method == 'POST':
        if post.favourite.filter(id=request.user.id).exists():
            post.favourite.remove(request.user)


        else:
            post.favourite.add(request.user)

    return HttpResponseRedirect(reverse('page:some_page', args=(fav_id,)))

The error I get is

AttributeError: 'WSGIRequest' object has no attribute 'user'

vendredi 26 avril 2019

How to report on Test Results by Date in Azure DevOps

There doesn't appear to be a way in Microsoft ADO to report on test results by date, ideally in a graph (Y-axis is test result, X-axis is date). Effectively, this is a testing burndown chart.

I scoured through all the available dashboard widgets, all the tabs inside of the Test Plans module, and the Analytics Views (I would use Power BI with those), and none of them allow you to do that. This seems crazy to me.

All of the test runs are there (Test Plans->Runs), so it's just a matter of surfacing those through one of the aforementioned mechanisms. Anyone have any ideas?

How can I effectively test multi-threaded, multi-process, multi-device code on Android?

I've written an Android wireless communication library that is, by nature, multi-threaded, multi-process, and (of course) multi-device.

Are there effective testing libraries/frameworks for code of this nature in the Android world?

  1. Unit testing the multi-threaded code is hard for obvious reasons. Unit tests don't play nice with threads. (For example) How do I unit test a function that needs to wait for a Broadcast?
  2. Most of the errors seem to be limited to the "real world". The library requires two devices to talk to each other. If I mock a perfect response sequence, it just feels like cheating. Nothing ever goes wrong, unlike in the real world. Also, the mocking process is complex and time consuming -- I write way more code mocking than in the actual library.
  3. All of the testing frameworks on Android, Instrumented or otherwise, are for one device at a time. I can't run simultaneous, co-dependent tests on two devices at once, can I?

I only have a system-level test at this time, and it's mostly Ad hoc. It's just a regular application with a simple controller script, that pushes the app to two devices, assigns their roles, runs the high-level functionality test, and polls for a log file on both devices. If a timeout is reached before the log files are written, the script returns an error code. If the logs ARE written, the communication sequence is checked for correctness. This test could, in theory, be added to CI, but it would need to run from a local test machine with physically connected devices. I can make this test more robust with time, but right now it's very fragile. Again, I made it for this specific project.

I'm unsure of my approach here. Are the any existing tools in the Android ecosystem that can help me test this kind of project? Any advice or push in the right direction would be appreciated.

CLIPS: testing correct facts

I have a series of rules and a set of initial (assert) facts. Now, I want to add those facts, run the rules, and then apply another set of rules to check if the current existing facts after (run) contains correct facts and nothing else, without triggering the previous rules anymore and without destroying current facts. Then, I want to keep going applying new facts, running new rules, and test new inserted facts, etc.

How can I do that? My test (batch) file is something like:

(clear) ; just in case
(load constructs.clp) ; All loaded in the MAIN module.

(assert (blabla))
(assert (blabla2))
(run)

;; Code (rules/functions... I'm still wondering how to do it) to check
;; current facts

(assert (blabla3))
(assert (blabla4))
(run)

;; More tests.

(exit)

I have tried to create, for each deftemplate T a deftemplate T-copy with same slots and them apply a (assert (testing)) fact to make the copies first. Then I fire a set of rules with testing purposes and higher salience that "halts" the (run) execution when it's done, to avoid firing the previous rules (the rules I'm testing). The problem with that approach, apart from requiring too many steps, is that I don't know the salience of the original rules and so I cannot be sure that the testing rules will have more priority.

I'm aware of the defmodule constructs and the focus stack but I haven't understood them yet. If my guesses are correct, I think I could put all of my testing rules in a specific module and place the focus on that module to avoid execution of any MAIN rule. If something is wrong, I'll (halt) execution in one of the testing rules or just (exit) the batch script. If everything is correct, I pop the testing module to come back to MAIN, add more assert, (run) again, and them push the testing module again with new tests to see if everything is still correct.

But I'm not sure if my assumptions are right I'll like to see an example of how should I do the testing.

PD: In addition, my CLIPS version has no support for fact-set queries.

Testing a component not developed interview -Testing,QA

I have been asked questions in interviews where they wanted to find out how to write test in advance for a component that is not ready yet. I answered it using TestNG Assertions/mock service point of view. Example, lets say Amazon's ship address component is ready but not shipping option (1 day,2 day, same day)- how can I write tests that will work once the component is ready.

Any thought on how you would have answered that question?

Drag & Drop Not Working in Selenium WebDriver For google.com

Can not drag the image of GOOGLE logo to Search field from www.google.com. Tried with Acion Class, even JavascriptExecutor.

        driver.get("https://www.google.com");

//      Element which needs to drag [Google Logo].          
        WebElement from=driver.findElement(By.id("hplogo"));    

//      Element on which need to drop [Google Search-bar].      
        WebElement to=driver.findElement(By.name("q"));

//      Using Action class for drag and drop.       
        Actions act=new Actions(driver);                    

//      Dragged and dropped.        
        act.dragAndDrop(from, to).build().perform();    

/*      JavascriptExecutor _js = (JavascriptExecutor)driver;
        _js.executeScript("$(arguments[0]).simulate('drag-n-drop', 
        {dragTarget:arguments[1],interpolation: 
        {stepWidth:100,stepDelay:50}});", from, to);             
*/

I want to drag & hold the Google Image, then want Drop image to the search box, but nothing happened. Manually if I drag and drop, I found image link like https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png in input serach box.

Even the below code works perfectly but not works for google.com!

public class DragAndDrop {
    public static void main(String args[]) throws InterruptedException
    {
        System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://jqueryui.com/resources/demos/droppable/default.html");
        Thread.sleep(10000);
        Actions act=new Actions(driver);
        WebElement drag = driver.findElement(By.xpath(".//*[@id='draggable']"));
        WebElement drop = driver.findElement(By.xpath(".//*[@id='droppable']"));
        act.dragAndDrop(drag, drop).build().perform();
    }