lundi 25 décembre 2017

Mapreduce MRUnit not passes test with correct output

I try to test my mapper in MaprReduce job with such test:

    @Test
    public void testMapper_1() throws IOException {
    mapDriver.withInput(new LongWritable(),
            new Text("ip981 - - [26/Apr/2011:14:52:47 -0400] \"GET /faq/ide.jpg HTTP/1.1\" 200 40416 \"http://host2/faq/\" \"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.205 Safari/534.16\""));
    mapDriver.withInput(new LongWritable(),
            new Text("ip987 - - [26/Apr/2011:14:52:50 -0400] \"GET /faq/scsi.gif HTTP/1.1\" 200 953 \"http://host2/faq/\" \"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13\""));

    MetricWritable value1 = new MetricWritable();
    value1.setValues(40416, 1.0);
    mapDriver.withOutput(new Text("ip981"), value1);

    MetricWritable value2 = new MetricWritable();
    value2.setValues(953, 1.0);
    mapDriver.withOutput(new Text("ip987"), value2);

    mapDriver.runTest();
    }

And recieve from MRUnit following error:

java.lang.AssertionError: 2 Error(s): (Missing expected output (ip981, 1.0  40416) at position 0, got (ip981, 1.0   40416)., Missing expected output (ip987, 1.0    953) at position 1, got (ip987, 1.0 953).)

My job works correctly with real input log. I suspect that problem is in whitespace and tab characters between fields of value. But I have no idea how to fix it. Could somebody help me to find way of fixing? Thank you.

Using QuickCheck with structures in Rust

I want to use QuickCheck to test a function that makes use of structs that I have created. I see I need to implement the Arbitrary trait for these types. I have something like this:

#[cfg(test)]
use quickcheck::{Arbitrary, Gen, QuickCheck};

const NUM_WORDS: usize = 12;

#[derive(Copy, Clone)]
pub struct Element(pub(crate) [u64; NUM_WORDS]);

#[cfg(test)]
impl Arbitrary for Element {
    fn arbitrary<G: Gen>(g: &mut G) -> Element {
        let e: [u64; NUM_WORDS] = g.gen::<[u64; NUM_WORDS]>();
        Element(e)
    }
}

#[derive(Clone, Debug)]
struct ExtensionElement {
    A: Element,
    B: Element,
}

#[cfg(test)]
impl Arbitrary for ExtensionElement {
    fn arbitrary<G: Gen>(g: &mut G) -> ExtensionElement {
        let a = g.gen::<Element>();
        let b = g.gen::<Element>();
        ExtensionElement { A: a, B: b }
    }
}

When I try to use ExtensionElement inside a QuickCheck test, I get the following error message inside the arbitrary function for ExtensionElement:

the trait 'rand::Rand' is not implemented for 'Element'

How I can implement the Arbitrary trait so that I can use my structs with QuickCheck?

How to guarantee corner cases in property based testing

Recently, I'm quite excited when reading about the idea of property based testing.

But I have 1 question that I still cannot find the answer anywhere:

How can property based testing ensures that it will test the corner cases every time?

To be more specific, let say I'm using ScalaCheck to test my division function:

def divide(a: Int, b: Int): Int

As the test cases will be generated randomly, how can I be sure that ScalaCheck will check the case where b = 0 every time?

dimanche 24 décembre 2017

Code Coverage and Ternary Operators

Consider we have this function under test located in the module.py:

def f(a, b):
    return (a - b) if a > b else 1 / 0

And, we have the following test case in the test_module.py:

from unittest import TestCase

from module import f


class ModuleTestCase(TestCase):
    def test_a_greater_than_b(self):
        self.assertEqual(f(10, 5), 5)

If we run tests with pytest with the enabled "branch coverage" with the HTML output reporting:

pytest test_module.py --cov=. --cov-branch --cov-report html

The report is going to claim 100% branch coverage with all the "partial" branches covered:

enter image description here

But, we clearly have not covered the else 1 / 0 part at all.

Is there a way to improve reporting to see the non-covered parts of the ternary operators?

Php - how to fail 'json_encode'?

Trying to test a method that is doing a json_encode and throwing an exception if the encode fails. However, whatever I throw at json_encode it successfully encodes. Any idea what would be something simple to have it fail?

Unit test for services

I have a services class UserServiceImpl with a method creatUser()

@Autowired
    private UserRepository  userRepository;

    @Autowired
    private RoleRepository roleRepository;

    @Override
    public User createUser(User user, Set<UserRole> userRoles) throws DataAccessException {
        User localUser = userRepository.findByUsername(user.getUsername());

        if (localUser != null) {
            LOG.info("user {} already exists. Nothing will be done.", user.getUsername());
        } else {
            for (UserRole ur : userRoles) {
                roleRepository.save(ur.getRole());
            }
            user.getUserRoles().addAll(userRoles);

            localUser = userRepository.save(user);
        }
        return localUser;
    }

I am trying to test this with unit test but don't know how to go about it . Any help would suffice .here is my test case .

 @Test
    public void createUserTest() {
        UserServiceImpl usr = new UserServiceImpl();
        User user = new User();

        String[] SET_VALUES = new String[]{"ADMIN"};

        UserRole userRole = new UserRole();

        userRole.setRole(role);
        Set<String> myset = new HashSet<String>(Arrays.asList(SET_VALUES));

        ObjectMapper objectMapper = new ObjectMapper();

        String[] GPXFILES1 = myset.toArray(new String[myset.size()]);

        usr.createUser(user,myset.toArray());


    }

right now I am stuck here , and I don't know how to pass a set as a parameter input

What is artificial intelligence ? How it's help or change on software testing field?

I am looking for some new technologies in software testing. Could you please explain what's is artificial intelligence ? How to start learning about this ? What are all pre requisite required ? How it's helpful in software testing ? .. please explain in simplest way